Storing BLOB when you don't know the length

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

I am currently using the following code to store a stream into a binary
database field. This works but I want to start reading from a compressed
stream where I will not know the length.

Is there any way to add the bytes chunk by chunk until there is none left to
read? Rather like the way that you extract the data.

I know I could compress it all to a memory stream first and then I would
know the length of the memory stream but that means using double the amount
of memory.

Code I am using to write the data
----------------------------------
byte[] Bytes = new byte[SourceStream.Length];
SourceStream.Read(Bytes, 0, Bytes.Length);
SQLCommand.Parameters.Add("@MyField", SqlDbType.Image).Value = Bytes;

Code I am using to read the data
----------------------------------
const int BufferSize = 1024;
byte[] Bytes = new byte[BufferSize];
long StartIndex = 0;
long ReadCount = 0;

do
{
StartIndex += ReadCount;
ReadCount = DataReader.GetBytes(0, StartIndex, Bytes, 0, BufferSize);
if (ReadCount > 0)
OutStream.Write(Bytes, 0, (int)ReadCount);
}
while (ReadCount == BufferSize);


Thanks
 
While it is possible to do this using Sql Server w/ READTEXT and UPDATETEXT,
it is kindof slow. Also, when you are writing/reading, you're doing so
using a fixed-length block size which you pre-determine by setting the Size
property on the parameter. This works great for a constant stream, but not
so great when your stream is going through compression methods which return
a smaller variable-length block. You have to write additional logic to
compensate for this and ensure you always send the same size block to Sql
Server for every UPDATETEXT command.

Robert
 
Back
Top