|
#region Using
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
#endregion
/*
* LOBSTREAM FRAMEWORK
* (c) Peter De Jonghe, Incalibration 2007
* Check out the website for the latest updates of the framework.
* http://www.incalibration.com
*
* The LobStream Framework code is free.
* You may use it as you wish, with the sole restriction that you maintain the copyright notice here included.
*
* Disclaimer:
* The LobStream Framework is provided "as is" without warranty of any kind, including, but not limited to,
* the implied warranties of merchantability and fitness for a particular purpose.
* The user assumes all responsibility for the accuracy and suitability of this program for a specific application.
* In no event will Incalibration, nor any of the authors of this code, be liable for any damages, including lost profits, lost savings,
* or other incidental or consequential damages arising from the use of or the inability to use this program.
*
* */
namespace LobStream
{
/// <summary>
///
/// </summary>
public abstract class SqlLobStream: LobStream
{
#region Member variables & Properties
private SqlConnection _connection = null;
/// <summary>
///
/// </summary>
public override IDbConnection Connection
{
get { return (SqlConnection)_connection; }
set { _connection = (SqlConnection)value; }
}
public abstract SqlCommand ReadData { get;set;}
private SqlCommand _validate = null;
/// <summary>
///
/// </summary>
public override IDbCommand Validate
{
get
{
if (this._validate == null)
{
string sqlIsValid = string.Format("select count({1}) from {0} where cast({1} as varchar(50)) = cast('{2}' as varchar(50))",
this.TableName, this.KeyColumn, this.LobID);
this._validate = new SqlCommand(sqlIsValid, this._connection);
}
return this._validate;
}
set
{
this._validate = (SqlCommand)value;
}
}
#region Length
private long _length = 0;
/// <summary>
///
/// </summary>
public override long Length
{
get
{
if (this._length == 0)
{
string sqlGetDataLength = string.Format("select @length = DATALENGTH({0}) from {1} where cast({2} as varchar(50)) = cast('{3}' as varchar(50))",
this.LobColumn, this.TableName, this.KeyColumn, this.LobID);
SqlCommand getDataLength = new SqlCommand(sqlGetDataLength, (SqlConnection)this.Connection);
// The SQL DataLength functions returns bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types
SqlParameter dataLength = getDataLength.Parameters.Add("@length", SqlDbType.BigInt);
dataLength.Direction = ParameterDirection.Output;
getDataLength.ExecuteNonQuery();
if (dataLength.Value == null || dataLength.Value == System.DBNull.Value) this._length = 0;
else this._length = Convert.ToInt64(dataLength.Value);
}
return _length;
}
}
#endregion
#endregion
#region WriteTo overload
/// <summary>
/// Writes the lob to a destination stream.
/// </summary>
/// <param name="destination">The stream to write to</param>
/// <param name="closeWhenFinished">Indicates if the lob stream must be closed afterwards</param>
/// <param name="bufferSize">The size of the buffer</param>
public override void WriteTo(Stream destination, bool closeWhenFinished, long bufferSize)
{
try
{
long bytesRead;
byte[] buffer = new byte[bufferSize];
this.Position = 0; //Reposition lob stream at the beginning ...
SqlDataReader sdr = this.ReadData.ExecuteReader(CommandBehavior.SequentialAccess);
sdr.Read();
while (true)
{
bytesRead = sdr.GetBytes(0, this.Position, buffer, 0, (int)bufferSize);
if (bytesRead == 0) break;
destination.Write(buffer, 0, (int)bytesRead);
this.Position += bytesRead;
}
sdr.Close();
destination.Flush();
}
catch (Exception ex)
{
throw new Exception("Write to stream operation failed.", ex);
}
finally
{
if (closeWhenFinished) this.Close();
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.