Esempio n. 1
0
        /// <summary>
        /// Writes the array into the BLOB column.
        /// </summary>
        /// <param name="buffer">The array to which bytes are written. </param>
        /// <param name="offset">The byte offset in array at which to begin writing.</param>
        /// <param name="count">The maximum number of bytes to write.</param>
        protected virtual void UpdateText(byte[] buffer, long offset, long count)
        {
            SqlContext	context	=	new SqlContext(this.ConnectionString);
            try
            {
                context.Transaction = this.Transaction;

                // Step 1. Create and Init BLOB data Pointer
                SqlParameter ptrParmPointer = new SqlParameter("@Pointer", SqlDbType.Binary, 16);
                ptrParmPointer.Value		= this.Pointer;

                // Step 2. Create and Init Offset
                SqlParameter ptrParmOffset = new SqlParameter("@Offset", SqlDbType.Int);
                ptrParmOffset.Value		= this.Position;

                // Step 3. Create and Init Count
                SqlParameter ptrParmCount = new SqlParameter("@Count", SqlDbType.Int);
                ptrParmCount.Value		= Math.Min(count,this.Length-this.Position);

                // Step 4. Create and Init Bytes
                SqlParameter ptrParmBytes = new SqlParameter("@Bytes", SqlDbType.Image);
                if(buffer.Length==count&&offset==0)
                {
                    ptrParmBytes.Value		= buffer;
                }
                else
                {
                    byte[] tmpBuffer	=	new byte[count];
                    Buffer.BlockCopy(buffer,(int)offset,tmpBuffer,0,(int)count);
                    ptrParmBytes.Value		= tmpBuffer;
                }

                SqlHelper.ExecuteNonQuery(context,CommandType.Text,GetUpdateTextCommand(),
                    ptrParmPointer,ptrParmOffset,ptrParmCount,ptrParmBytes);

                this.Position	+=	count;
                if(this.Position>this.Length)
                    this._lenght = this.Position;
            }
            finally
            {
                context.Transaction = null;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Writes the array into the BLOB column.
        /// </summary>
        /// <param name="buffer">The array to which bytes are written. </param>
        /// <param name="offset">The byte offset in array at which to begin writing.</param>
        /// <param name="count">The maximum number of bytes to write.</param>
        protected virtual void UpdateTextCommandWithNullValue(byte[] buffer, long offset, long count)
        {
            SqlContext	context	=	new SqlContext(this.ConnectionString);
            try
            {
                context.Transaction = this.Transaction;

                // Step 4. Create and Init Bytes
                SqlParameter ptrParmBytes = new SqlParameter(string.Format("@{0}",this.ColumnName), SqlDbType.Image);
                if(buffer.Length==count&&offset==0)
                {
                    ptrParmBytes.Value		= buffer;
                }
                else
                {
                    byte[] tmpBuffer	=	new byte[count];
                    Buffer.BlockCopy(buffer,(int)offset,tmpBuffer,0,(int)count);
                    ptrParmBytes.Value		= tmpBuffer;
                }

                SqlHelper.ExecuteNonQuery(context,CommandType.Text,GetUpdateTextCommandWithNullValue(),
                    this.PrimaryKeys.ToArray(ptrParmBytes));

                this.Position	+=	count;
                if(this.Position>this.Length)
                    this._lenght = this.Position;
            }
            finally
            {
                context.Transaction = null;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Opens the text pointer.
        /// </summary>
        /// <returns>The text pointer.</returns>
        protected virtual byte[] OpenPointer()
        {
            byte[] retVal	=	null;

            SqlContext	context	=	new SqlContext(this.ConnectionString);
            try
            {
                context.Transaction = this.Transaction;

                SqlParameter ptrParm = new SqlParameter("@Pointer", SqlDbType.Binary, 16);
                ptrParm.Direction = ParameterDirection.Output;

                SqlHelper.ExecuteNonQuery(context,CommandType.Text,GetOpenPointerCommand(),this.PrimaryKeys.ToArray(ptrParm));

                if(ptrParm.Value!=DBNull.Value)
                    retVal	=	(byte[])ptrParm.Value;
                else
                    retVal	=	SqlBlobStream.DBNullPointer;
            }
            finally
            {
                context.Transaction = null;
            }
            return retVal;
        }
Esempio n. 4
0
        /// <summary>
        /// Reads the array from the BLOB column.
        /// </summary>
        /// <param name="buffer">When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
        /// <param name="offset">The byte offset in array at which to begin reading.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <returns>The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.</returns>
        protected virtual int ReadText(byte[] buffer, int offset, int count)
        {
            int retVal	=	0;

            SqlContext	context	=	new SqlContext(this.ConnectionString);
            try
            {
                context.Transaction = this.Transaction;

                // Step 1. Create and Init BLOB data Pointer
                SqlParameter ptrParmPointer = new SqlParameter("@Pointer", SqlDbType.Binary, 16);
                ptrParmPointer.Value		= this.Pointer;

                // Step 2. Create and Init Offset
                SqlParameter ptrParmOffset = new SqlParameter("@Offset", SqlDbType.Int);
                ptrParmOffset.Value		= this.Position;

                // Step 2. Create and Init Bytes
                SqlParameter ptrParmBytes = new SqlParameter("@Bytes", SqlDbType.Int);
                int bCount		= (int)(Math.Min(count,this.Length-this.Position));
                ptrParmBytes.Value	= bCount;

                if(bCount>0)
                {
                    using(IDataReader reader = SqlHelper.ExecuteReader(context,CommandType.Text,GetReadTextCommand(),
                              ptrParmPointer,ptrParmOffset,ptrParmBytes))
                    {
                        if(reader.Read())
                        {
                            byte[] tmpBuffer	=	(byte[])reader[this.ColumnName];

                            retVal	=	tmpBuffer.Length;
                            Buffer.BlockCopy(tmpBuffer,0,buffer,offset,retVal);
                        }
                    }

                    this.Position	+=	retVal;
                }
            }
            finally
            {
                context.Transaction = null;
            }

            return retVal;
        }
Esempio n. 5
0
        /// <summary>
        /// Overridden. Sets the length of this stream to the given value.
        /// </summary>
        /// <param name="value">The new length of the stream. </param>
        public override void SetLength(long value)
        {
            if(this.Pointer==null)
                throw new ObjectDisposedException(null,"The stream is closed.");

            if(this.CanWrite)
            {
                if(value!=this.Length)
                {
                    SqlContext	context	=	new SqlContext(this.ConnectionString);
                    try
                    {
                        context.Transaction = this.Transaction;

                        // Step 1. Create and Init BLOB data Pointer
                        SqlParameter ptrParmPointer = new SqlParameter("@Pointer", SqlDbType.Binary, 16);
                        ptrParmPointer.Value		= this.Pointer;

                        if(value<this.Length)
                        {
                            // Step 2. Create and Init Offset
                            SqlParameter ptrParmOffset = new SqlParameter("@Offset", SqlDbType.Int);
                            ptrParmOffset.Value		= value;

                            // Step 3. Create and Init Count
                            SqlParameter ptrParmCount = new SqlParameter("@Count", SqlDbType.Int);
                            ptrParmCount.Value		= this.Length - value;

                            // Step 4. Create and Init Bytes
                            SqlParameter ptrParmBytes = new SqlParameter("@Bytes", SqlDbType.Image);
                            ptrParmBytes.Value		= new byte[]{};

                            SqlHelper.ExecuteNonQuery(context,CommandType.Text,GetUpdateTextCommand(),
                                ptrParmPointer,ptrParmOffset,ptrParmCount,ptrParmBytes);

                            this._lenght = value;
                            this.Position = Math.Min(this.Position,this.Length);
                        }
                        else
                        {
                            // Step 2. Create and Init Offset
                            SqlParameter ptrParmOffset = new SqlParameter("@Offset", SqlDbType.Int);
                            ptrParmOffset.Value		= this.Length;

                            // Step 3. Create and Init Count
                            SqlParameter ptrParmCount = new SqlParameter("@Count", SqlDbType.Int);
                            ptrParmCount.Value		= 0;

                            // Step 4. Create and Init Bytes
                            SqlParameter ptrParmBytes = new SqlParameter("@Bytes", SqlDbType.Image);
                            ptrParmBytes.Value		= new byte[(value-this.Length)];

                            SqlHelper.ExecuteNonQuery(context,CommandType.Text,GetUpdateTextCommand(),
                                ptrParmPointer,ptrParmOffset,ptrParmCount,ptrParmBytes);

                            this._lenght = value;
                        }
                    }
                    finally
                    {
                        context.Transaction = null;
                    }
                }
            }
            else
            {
                throw new NotSupportedException("SqlBlobStream is Readonly.");
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the selected element's data length.
        /// </summary>
        /// <returns>The data length. (bytes)</returns>
        protected virtual int GetDataLength()
        {
            int retVal	=	0;

            SqlContext	context	=	new SqlContext(this.ConnectionString);
            try
            {
                context.Transaction = this.Transaction;

                SqlParameter ptrParm = new SqlParameter("@DataLength", SqlDbType.Int);
                ptrParm.Direction = ParameterDirection.Output;

                SqlHelper.ExecuteNonQuery(context,CommandType.Text,GetDataLengthCommand(),this.PrimaryKeys.ToArray(ptrParm));

                if(ptrParm.Value!=DBNull.Value)
                    retVal	=	(int)ptrParm.Value;
            }
            finally
            {
                context.Transaction = null;
            }

            return retVal;
        }
Esempio n. 7
0
 /// <summary>
 /// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in 
 /// the connection string. 
 /// </summary>
 /// <param name="context">the current context/</param>
 /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
 /// <param name="commandText">the stored procedure name or T-SQL command</param>
 /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
 /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
 public static object ExecuteScalar(SqlContext context, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 {
     if(context.Transaction!=null)
         return ExecuteScalar(context.Transaction, commandType, commandText, commandParameters);
     else
         return ExecuteScalar(context.ConnectionString, commandType, commandText, commandParameters);
 }
Esempio n. 8
0
 /// <summary>
 /// Create and prepare a SqlCommand, and call ExecuteReader with the appropriate CommandBehavior.
 /// </summary>
 /// <param name="context">the current context.</param>
 /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
 /// <param name="commandText">the stored procedure name or T-SQL command</param>
 /// <returns>SqlDataReader containing the results of the command</returns>
 public static SqlDataReader ExecuteReader(SqlContext context, CommandType commandType, string commandText)
 {
     return ExecuteReader(context, commandType, commandText, (SqlParameter[])null);
 }
Esempio n. 9
0
 /// <summary>
 /// Execute a SqlCommand (that returns no resultset and takes no parameters) against the database specified in 
 /// the connection string. 
 /// </summary>
 /// <param name="context">The current context.</param>
 /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
 /// <param name="commandText">the stored procedure name or T-SQL command</param>
 /// <returns>an int representing the number of rows affected by the command</returns>
 public static int ExecuteNonQuery(SqlContext context, CommandType commandType, string commandText)
 {
     //pass through the call providing null for the set of SqlParameters
     return ExecuteNonQuery(context, commandType, commandText, (SqlParameter[])null);
 }
Esempio n. 10
0
        /// <summary>
        /// Executes a complex sql script.
        /// </summary>
        /// <param name="context">The current context.</param>
        /// <param name="Script">A sql script.</param>
        public static void ExecuteScript(SqlContext context, string Script)
        {
            #if DEBUG
            System.Diagnostics.Trace.WriteLine(Script,"ExecuteScript");
            #endif
            using(System.IO.StringReader	reader = new System.IO.StringReader(Script))
            {
                bool bContueRead = true;
                StringBuilder	sqlCommandText	=	new StringBuilder();

                while(bContueRead)
                {
                    while(true)
                    {
                        string line = reader.ReadLine();

                        if(line==null)
                        {
                            bContueRead = false;
                            break;
                        }

                        string trimLine = line.Trim();

                        if(trimLine!=string.Empty)
                        {
                            if(string.Compare(trimLine,"GO",true)==0)
                                break;
                            else
                            {
                                sqlCommandText.Append(line);
                                sqlCommandText.Append("\r\n");
                            }
                        }
                    }

                    if(sqlCommandText.Length>0)
                    {
                        SqlHelper.ExecuteNonQuery(context,CommandType.Text,sqlCommandText.ToString());
                        sqlCommandText  =	new StringBuilder();
                    }
                }

            }
        }
Esempio n. 11
0
        /// <summary>
        /// Overridden. Sets the length of this stream to the given value.
        /// </summary>
        /// <param name="value">The new length of the stream. </param>
        public override void SetLength(long value)
        {
            if (this.Pointer == null)
            {
                throw new ObjectDisposedException(null, "The stream is closed.");
            }

            if (this.CanWrite)
            {
                if (value != this.Length)
                {
                    SqlContext context = new SqlContext(this.ConnectionString);
                    try
                    {
                        context.Transaction = this.Transaction;

                        // Step 1. Create and Init BLOB data Pointer
                        SqlParameter ptrParmPointer = new SqlParameter("@Pointer", SqlDbType.Binary, 16);
                        ptrParmPointer.Value = this.Pointer;

                        if (value < this.Length)
                        {
                            // Step 2. Create and Init Offset
                            SqlParameter ptrParmOffset = new SqlParameter("@Offset", SqlDbType.Int);
                            ptrParmOffset.Value = value;

                            // Step 3. Create and Init Count
                            SqlParameter ptrParmCount = new SqlParameter("@Count", SqlDbType.Int);
                            ptrParmCount.Value = this.Length - value;

                            // Step 4. Create and Init Bytes
                            SqlParameter ptrParmBytes = new SqlParameter("@Bytes", SqlDbType.Image);
                            ptrParmBytes.Value = new byte[] {};

                            SqlHelper.ExecuteNonQuery(context, CommandType.Text, GetUpdateTextCommand(),
                                                      ptrParmPointer, ptrParmOffset, ptrParmCount, ptrParmBytes);

                            this._lenght  = value;
                            this.Position = Math.Min(this.Position, this.Length);
                        }
                        else
                        {
                            // Step 2. Create and Init Offset
                            SqlParameter ptrParmOffset = new SqlParameter("@Offset", SqlDbType.Int);
                            ptrParmOffset.Value = this.Length;

                            // Step 3. Create and Init Count
                            SqlParameter ptrParmCount = new SqlParameter("@Count", SqlDbType.Int);
                            ptrParmCount.Value = 0;

                            // Step 4. Create and Init Bytes
                            SqlParameter ptrParmBytes = new SqlParameter("@Bytes", SqlDbType.Image);
                            ptrParmBytes.Value = new byte[(value - this.Length)];

                            SqlHelper.ExecuteNonQuery(context, CommandType.Text, GetUpdateTextCommand(),
                                                      ptrParmPointer, ptrParmOffset, ptrParmCount, ptrParmBytes);

                            this._lenght = value;
                        }
                    }
                    finally
                    {
                        context.Transaction = null;
                    }
                }
            }
            else
            {
                throw new NotSupportedException("SqlBlobStream is Readonly.");
            }
        }