PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Storing BLOB when you don't know the length

Reply

Storing BLOB when you don't know the length

 
Thread Tools Rate Thread
Old 15-09-2006, 09:53 AM   #1
Andrew
Guest
 
Posts: n/a
Default Storing BLOB when you don't know the length


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

--
Andrew Cutforth - AJC Software - www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.


  Reply With Quote
Old 15-09-2006, 04:55 PM   #2
Robert Simpson
Guest
 
Posts: n/a
Default Re: Storing BLOB when you don't know the length

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


"Andrew" <someone@nospam.com> wrote in message
news:eQkmnRK2GHA.4484@TK2MSFTNGP02.phx.gbl...
>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
>
> --
> Andrew Cutforth - AJC Software - www.ajcsoft.com
> The best folder synchronize and directory compare tool available.
> AJC Active Backup instantly archives every file you edit giving you
> unlimited undo and automatic revision control. Never lose your data
> again.
>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off