SaaS CMS has officially launched! Learn more now.

Class SqlBlobStream

Exposes a Stream around a text, ntext, or image column, supporting both synchronous and asynchronous read and write operations.

Inheritance
System.Object
System.MarshalByRefObject
System.IO.Stream
SqlBlobStream
Implements
System.IDisposable
Inherited Members
System.IO.Stream.Null
System.IO.Stream.CopyToAsync(System.IO.Stream)
System.IO.Stream.CopyToAsync(System.IO.Stream, System.Int32)
System.IO.Stream.CopyToAsync(System.IO.Stream, System.Int32, System.Threading.CancellationToken)
System.IO.Stream.CopyTo(System.IO.Stream)
System.IO.Stream.CopyTo(System.IO.Stream, System.Int32)
System.IO.Stream.Dispose()
System.IO.Stream.Dispose(System.Boolean)
System.IO.Stream.FlushAsync()
System.IO.Stream.FlushAsync(System.Threading.CancellationToken)
System.IO.Stream.CreateWaitHandle()
System.IO.Stream.BeginRead(System.Byte[], System.Int32, System.Int32, System.AsyncCallback, System.Object)
System.IO.Stream.EndRead(System.IAsyncResult)
System.IO.Stream.ReadAsync(System.Byte[], System.Int32, System.Int32)
System.IO.Stream.ReadAsync(System.Byte[], System.Int32, System.Int32, System.Threading.CancellationToken)
System.IO.Stream.BeginWrite(System.Byte[], System.Int32, System.Int32, System.AsyncCallback, System.Object)
System.IO.Stream.EndWrite(System.IAsyncResult)
System.IO.Stream.WriteAsync(System.Byte[], System.Int32, System.Int32)
System.IO.Stream.WriteAsync(System.Byte[], System.Int32, System.Int32, System.Threading.CancellationToken)
System.IO.Stream.ReadByte()
System.IO.Stream.WriteByte(System.Byte)
System.IO.Stream.Synchronized(System.IO.Stream)
System.IO.Stream.ObjectInvariant()
System.IO.Stream.CanTimeout
System.IO.Stream.ReadTimeout
System.IO.Stream.WriteTimeout
System.MarshalByRefObject.MemberwiseClone(System.Boolean)
System.MarshalByRefObject.GetLifetimeService()
System.MarshalByRefObject.InitializeLifetimeService()
System.MarshalByRefObject.CreateObjRef(System.Type)
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: Mediachase.BusinessFoundation.Data.Sql
Assembly: Mediachase.BusinessFoundation.Data.dll
Version: 11.8.3
Syntax
public class SqlBlobStream : Stream, IDisposable
Remarks

A common practice to reduce the amount of memory used when writing a BLOB value is to write the BLOB to the database in "chunks". The process of writing a BLOB to a database in this way depends on the capabilities of your database.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Use the SqlBlobStream class to read, write, create or close text, ntext, or image values from a text, ntext, or image column, as well as to manipulate other file-related operating system handles such as file.

SqlBlobStream objects support random access to files using the Seek method. Seek allows the read/write position to be moved to any position within the file. This is done with byte offset reference point parameters. The byte offset is relative to the seek reference point, which can be the beginning, the current position, or the end of the underlying file, as represented by the three properties of the SeekOrigin class.

For an example of using this class, see the Example section below.

Examples

The following SqlBlobStream constructor grants read-only access to an column (SqlBlobAccess.Read).

string strConnectionString = "Data source=(local);Initial catalog=TestDB;User Id=sa;Password=";
string strSqlTable = "Files";
string strBLOBColumn = "Data";

try
{
	using(SqlBlobStream stream = new SqlBlobStream(strConnectionString,
			strSqlTable,
			strBLOBColumn,
			SqlBlobAccess.Read,new SqlParameter("@FileId",12345)))
	{
		byte[] tmpBuffer = new byte[1024];
		int Length = 0;
		while((Length=stream.Read(buffer,0,1024))==1024)
		{
			//TODO: Save tmpBuffer
		}
	}
}
catch(Exception ex)
{
	System.Diagnostics.Trace.WriteLine(ex);
	throw;
}

Constructors

SqlBlobStream()

Initializes a new instance of SqlBlobStream class.

Declaration
public SqlBlobStream()
Remarks

Invoke Mediachase.FileUploader.SqlBlobStream.Open method to open stream.

SqlBlobStream(String, String, SqlBlobAccess, SqlParameter[])

Initializes a new instance of the SqlBlobStream class.

Declaration
public SqlBlobStream(string TableName, string BlobDataColumn, SqlBlobAccess BlobAccess, params SqlParameter[] PrimaryKeys)
Parameters
Type Name Description
System.String TableName

Name of the table.

System.String BlobDataColumn

The BLOB data column.

SqlBlobAccess BlobAccess

The BLOB access.

System.Data.SqlClient.SqlParameter[] PrimaryKeys

The primary keys.

SqlBlobStream(String, String, SqlBlobAccess, String, Guid)

Initializes a new instance of the SqlBlobStream class.

Declaration
public SqlBlobStream(string TableName, string BlobDataColumn, SqlBlobAccess BlobAccess, string PrimaryKeyColumn, Guid PrimaryKey)
Parameters
Type Name Description
System.String TableName

Name of the table.

System.String BlobDataColumn

The BLOB data column.

SqlBlobAccess BlobAccess

The BLOB access.

System.String PrimaryKeyColumn

The primary key column.

System.Guid PrimaryKey

The primary key.

SqlBlobStream(String, String, SqlBlobAccess, String, Int32)

Initializes a new instance of the SqlBlobStream class.

Declaration
public SqlBlobStream(string TableName, string BlobDataColumn, SqlBlobAccess BlobAccess, string PrimaryKeyColumn, int PrimaryKey)
Parameters
Type Name Description
System.String TableName

Name of the table.

System.String BlobDataColumn

The BLOB data column.

SqlBlobAccess BlobAccess

The BLOB access.

System.String PrimaryKeyColumn

The primary key column.

System.Int32 PrimaryKey

The primary key.

Properties

BlobAccess

Gets a current read/write permission.

Declaration
public virtual SqlBlobAccess BlobAccess { get; }
Property Value
Type Description
SqlBlobAccess

CanRead

Overridden. Gets a value indicating whether the current stream supports reading.

Declaration
public override bool CanRead { get; }
Property Value
Type Description
System.Boolean
Overrides
System.IO.Stream.CanRead

CanSeek

Overridden. Gets a value indicating whether the current stream supports seeking.

Declaration
public override bool CanSeek { get; }
Property Value
Type Description
System.Boolean
Overrides
System.IO.Stream.CanSeek

CanWrite

Overridden. Gets a value indicating whether the current stream supports writing.

Declaration
public override bool CanWrite { get; }
Property Value
Type Description
System.Boolean
Overrides
System.IO.Stream.CanWrite

ColumnName

Gets, sets a table column for the BLOB value that the current SqlBlobStream object will encapsulate.

Declaration
public virtual string ColumnName { get; set; }
Property Value
Type Description
System.String

IsOpen

Gets the current SqlBlobStream's state.

Declaration
public bool IsOpen { get; }
Property Value
Type Description
System.Boolean

Length

Overridden. Gets the length in bytes of the stream.

Declaration
public override long Length { get; }
Property Value
Type Description
System.Int64
Overrides
System.IO.Stream.Length

Pointer

Gets a current pointer to the BLOB value.

Declaration
public byte[] Pointer { get; }
Property Value
Type Description
System.Byte[]

Position

Overridden. Gets or sets the current position of this stream.

Declaration
public override long Position { get; set; }
Property Value
Type Description
System.Int64
Overrides
System.IO.Stream.Position

PrimaryKeys

Gets the primary keys collection.

Declaration
public virtual SqlParameterList PrimaryKeys { get; }
Property Value
Type Description
SqlParameterList

TableName

Gets, sets a table name for the table that the current SqlBlobStream object will encapsulate.

Declaration
public virtual string TableName { get; set; }
Property Value
Type Description
System.String

Methods

Close()

Overridden. Closes the file and releases any resources associated with the current file stream.

Declaration
public override void Close()
Overrides
System.IO.Stream.Close()

Flush()

Overridden. Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

Declaration
public override void Flush()
Overrides
System.IO.Stream.Flush()

GetDataLength()

Gets the selected element's data length.

Declaration
protected virtual int GetDataLength()
Returns
Type Description
System.Int32

The data length. (bytes)

GetDataLengthCommand()

Gets the sql command returning Data Length.

Declaration
protected virtual string GetDataLengthCommand()
Returns
Type Description
System.String

The sql command.

GetOpenPointerCommand()

Gets the sql command returning a text pointer.

Declaration
protected virtual string GetOpenPointerCommand()
Returns
Type Description
System.String

The sql command.

GetReadTextCommand()

Gets the sql command reading a blob element.

Declaration
protected virtual string GetReadTextCommand()
Returns
Type Description
System.String

The sql command.

GetUpdateTextCommand()

Gets the sql command updating a blob element.

Declaration
protected virtual string GetUpdateTextCommand()
Returns
Type Description
System.String

The sql command.

GetUpdateTextCommandWithNullValue()

Gets the sql command inserting a blob element.

Declaration
protected virtual string GetUpdateTextCommandWithNullValue()
Returns
Type Description
System.String

The sql command.

Init()

Initializes a new connection.

Declaration
protected void Init()

OnClose(Boolean)

Releases the resources used by the SqlBlobStream.

Declaration
protected virtual void OnClose(bool disposing)
Parameters
Type Name Description
System.Boolean disposing

The disposing flag.

OnInit()

Raises the Init event.

Declaration
protected virtual void OnInit()

Open()

Opens the closed connection.

Declaration
public void Open()

OpenPointer()

Opens the text pointer.

Declaration
protected virtual byte[] OpenPointer()
Returns
Type Description
System.Byte[]

The text pointer.

Read(Byte[], Int32, Int32)

Overridden. Reads a block of bytes from the stream and writes the data in a given buffer.

Declaration
public override int Read(byte[] buffer, int offset, int count)
Parameters
Type Name Description
System.Byte[] 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.

System.Int32 offset

The byte offset in array at which to begin reading.

System.Int32 count

The maximum number of bytes to read.

Returns
Type Description
System.Int32

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.

Overrides
System.IO.Stream.Read(System.Byte[], System.Int32, System.Int32)

ReadText(Byte[], Int32, Int32)

Reads the array from the BLOB column.

Declaration
protected virtual int ReadText(byte[] buffer, int offset, int count)
Parameters
Type Name Description
System.Byte[] 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.

System.Int32 offset

The byte offset in array at which to begin reading.

System.Int32 count

The maximum number of bytes to read.

Returns
Type Description
System.Int32

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.

Seek(Int64, SeekOrigin)

Overridden. Sets the current position of this stream to the given value.

Declaration
public override long Seek(long offset, SeekOrigin origin)
Parameters
Type Name Description
System.Int64 offset

The point relative to origin from which to begin seeking.

System.IO.SeekOrigin origin

Specifies the beginning, the end, or the current position as a reference point for origin, using a value of type SeekOrigin.

Returns
Type Description
System.Int64

The new position in the stream.

Overrides
System.IO.Stream.Seek(System.Int64, System.IO.SeekOrigin)

SetLength(Int64)

Overridden. Sets the length of this stream to the given value.

Declaration
public override void SetLength(long value)
Parameters
Type Name Description
System.Int64 value

The new length of the stream.

Overrides
System.IO.Stream.SetLength(System.Int64)

UpdateText(Byte[], Int64, Int64)

Writes the array into the BLOB column.

Declaration
protected virtual void UpdateText(byte[] buffer, long offset, long count)
Parameters
Type Name Description
System.Byte[] buffer

The array to which bytes are written.

System.Int64 offset

The byte offset in array at which to begin writing.

System.Int64 count

The maximum number of bytes to write.

UpdateTextCommandWithNullValue(Byte[], Int64, Int64)

Writes the array into the BLOB column.

Declaration
protected virtual void UpdateTextCommandWithNullValue(byte[] buffer, long offset, long count)
Parameters
Type Name Description
System.Byte[] buffer

The array to which bytes are written.

System.Int64 offset

The byte offset in array at which to begin writing.

System.Int64 count

The maximum number of bytes to write.

Write(Byte[], Int32, Int32)

Overridden. Writes a block of bytes to this stream using data from a buffer.

Declaration
public override void Write(byte[] buffer, int offset, int count)
Parameters
Type Name Description
System.Byte[] buffer

The array to which bytes are written.

System.Int32 offset

The byte offset in array at which to begin writing.

System.Int32 count

The maximum number of bytes to write.

Overrides
System.IO.Stream.Write(System.Byte[], System.Int32, System.Int32)

Implements

System.IDisposable