Class SqlBlobStream
Exposes a Stream around a text, ntext, or image column, supporting both synchronous and asynchronous read and write operations.
Inheritance
Implements
Inherited Members
Namespace: Mediachase.BusinessFoundation.Data.Sql
Assembly: Mediachase.BusinessFoundation.Data.dll
Version: 12.17.2Syntax
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
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
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
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
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
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
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
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
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
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
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. |