Home All Groups Group Topic Archive Search About

Blob reading problems

Author
2 Jul 2006 4:33 PM
excession
Hi

I'm trying to read RTF and Word files from blob columns in SQL Server, but
the files can't be opened in Word when i retreive them. They seem to contain
garbage... What am I doing wrong?

This is some of the code used to get the files:

FileStream fileStream = new FileStream(filename, FileMode.Create,
FileAccess.Write);
BinaryWriter binaryWriter = new BinaryWriter(fileStream);

long totalBytes = sqlDataReader.GetBytes(blobColumn, 0, null, 0,
int.MaxValue);

byte[] outbyte = new byte[totalBytes];
sqlDataReader.GetBytes(blobColumn, 0, outbyte, 0, (int)totalBytes);
binaryWriter.Write(outbyte, 0, (int)totalBytes);
binaryWriter.Flush();

binaryWriter.Close();
fileStream.Close();

Author
9 Jul 2006 9:02 AM
Joerg Jooss
Thus wrote excession,

Show quote
> Hi
>
> I'm trying to read RTF and Word files from blob columns in SQL Server,
> but the files can't be opened in Word when i retreive them. They seem
> to contain garbage... What am I doing wrong?
>
> This is some of the code used to get the files:
>
> FileStream fileStream = new FileStream(filename, FileMode.Create,
> FileAccess.Write);
> BinaryWriter binaryWriter = new BinaryWriter(fileStream);
> long totalBytes = sqlDataReader.GetBytes(blobColumn, 0, null, 0,
> int.MaxValue);
>
> byte[] outbyte = new byte[totalBytes];
> sqlDataReader.GetBytes(blobColumn, 0, outbyte, 0, (int)totalBytes);
> binaryWriter.Write(outbyte, 0, (int)totalBytes); binaryWriter.Flush();
>
> binaryWriter.Close();
> fileStream.Close();

Using a BinaryWriter for arbitrary binary content is a potentially destructive
operation. To copy the data "as is", write it directly to your FileStream.

Cheers,
--
Joerg Jooss
news-re***@joergjooss.de

AddThis Social Bookmark Button