/// <summary>Writes the contents of the buffer to the VarBinary column and clears the buffer.</summary>
        public override void Flush()
        {
            //If we're in read mode (or no mode), then we don't want to flush the data.
            if (!this.IsReading.HasValue || this.IsReading.Value)
            {
                return;
            }

            //If there is nothing to flush then don't do anything.
            if (this.BufferPosition == 0)
            {
                return;
            }

            byte[] bytesToWrite = new byte[this.BufferPosition];

            //Copy from the buffer.
            System.Buffer.BlockCopy(Buffer, 0, bytesToWrite, 0, this.BufferPosition);

            //Write the bytes to the column and update the position.
            this.Position += SqlVarBinaryMaxHelper.WritePartOfVarBinaryColumn(this.Connection, this.TableName, this.ColumnName, this.PrimaryKeyColumnName, this.PrimaryKeyValue, this.Position, bytesToWrite);

            //Clear the buffer.
            this.Buffer         = new byte[SqlVarBinaryStream.BufferSize];
            this.BufferPosition = 0;
        }
        /// <summary>Sets the column's data to 0x0. Otherwise, if you were to write a smaller file to the column than the existing data,the existing data would still remain after the smaller data is written.</summary>
        private void ClearColumnData()
        {
            //Initialize (clear) the column.
            SqlVarBinaryMaxHelper.InitializeVarBinaryColumn(this.Connection, this.TableName, this.ColumnName, this.PrimaryKeyColumnName, this.PrimaryKeyValue);

            //Indicate that the data no longer needs to be cleared.
            this.ClearData = false;
        }
        /// <summary>Reads data from the VarBinary column into the supplied buffer.</summary>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (!this.IsReading.HasValue)
            {
                this.IsReading = true;
            }
            else if (!this.IsReading.Value)
            {
                throw new InvalidOperationException("The stream is already being used to write data; it cannot be used to read data as well.");
            }

            int bytesToWrite = count;
            int bytesWritten = 0;

            do
            {
                int bytesToCopy = 0;

                //If the buffer is empty, read another chunk of data.
                if (Buffer == null || (this.Buffer.Length - this.BufferPosition) == 0)
                {
                    byte[] newBytes  = SqlVarBinaryMaxHelper.ReadPartOfVarbinaryColumn(this.Connection, this.TableName, this.ColumnName, this.PrimaryKeyColumnName, this.PrimaryKeyValue, this.Position, SqlVarBinaryStream.BufferSize);
                    int    bytesRead = (newBytes == null ? 0 : newBytes.Length);

                    if (bytesRead > 0)
                    {
                        this.Buffer         = new byte[newBytes.Length];
                        this.BufferPosition = 0;
                        newBytes.CopyTo(Buffer, 0);
                        this.Position += bytesRead;
                    }
                    else
                    {
                        break;
                    }
                }

                //Copy to the output buffer.
                bytesToCopy = System.Math.Min(bytesToWrite, this.Buffer.Length - this.BufferPosition);
                System.Buffer.BlockCopy(this.Buffer, this.BufferPosition, buffer, offset + bytesWritten, bytesToCopy);

                //Update the counters.
                bytesToWrite        -= bytesToCopy;
                bytesWritten        += bytesToCopy;
                this.BufferPosition += bytesToCopy;
            } while (bytesWritten < count);

            return(bytesWritten);
        }
        private SqlVarBinaryStream(SqlConnection connection, string tableName, string columnName, string primaryKeyColumnName, int primaryKeyValue)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("tableName");
            }

            if (string.IsNullOrEmpty(columnName))
            {
                throw new ArgumentNullException("columnName");
            }

            this.Connection           = connection;
            this.TableName            = tableName;
            this.ColumnName           = columnName;
            this.PrimaryKeyColumnName = primaryKeyColumnName;
            this.PrimaryKeyValue      = primaryKeyValue;
            this.LengthInternal       = SqlVarBinaryMaxHelper.GetLengthOfVarbinaryColumn(this.Connection, this.TableName, this.ColumnName, this.PrimaryKeyColumnName, this.PrimaryKeyValue);
            this.ClearData            = true;
        }