Read Word docs from Image (BLOB) Column in SQL Server

G

Guest

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();
 
P

Paul Clement

¤ 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();

Do you know how they were added to the database? Were they added as OLE objects?


Paul
~~~~
Microsoft MVP (Visual Basic)
 
G

Guest

Yes, they were added as OLE objects.

I thought that I could simply pull the objects from the database and open
them without any hassle.. but I haven't had any luck yet.
 
G

Guest

Actually, if I open one of the files in notepad and scroll through the doc I
can read some of the original text.

Quite a lot of it is formattet I guess.. And quite a lot has a square char
between chars in words.

excession said:
Yes, they were added as OLE objects.

I thought that I could simply pull the objects from the database and open
them without any hassle.. but I haven't had any luck yet.

Paul Clement said:
¤ 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();

Do you know how they were added to the database? Were they added as OLE objects?


Paul
~~~~
Microsoft MVP (Visual Basic)
 
P

Paul Clement

¤ Yes, they were added as OLE objects.
¤
¤ I thought that I could simply pull the objects from the database and open
¤ them without any hassle.. but I haven't had any luck yet.
¤

You have to strip the OLE header information before saving to a file. You might want to see if the
following helps:

http://sastools.com/b2/post/79394244


Paul
~~~~
Microsoft MVP (Visual Basic)
 
P

Paul Clement

¤ I'm afraid that didn't help.
¤
¤ Where does Word OLE header end? 78 didn't work..
¤

Give 85 a try. If that doesn't work I'll take a closer look at it.


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top