Home All Groups Group Topic Archive Search About

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

Author
2 Jul 2006 4:12 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
5 Jul 2006 4:45 PM
Paul Clement
On Sun, 2 Jul 2006 09:12:01 -0700, excession <excess***@discussions.microsoft.com> wrote:

¤ 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)
Author
5 Jul 2006 5:07 PM
excession
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.

Show quote
"Paul Clement" wrote:

> On Sun, 2 Jul 2006 09:12:01 -0700, excession <excess***@discussions.microsoft.com> wrote:
>
> ¤ 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)
>
Author
5 Jul 2006 7:10 PM
excession
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.

Show quote
"excession" wrote:

> 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" wrote:
>
> > On Sun, 2 Jul 2006 09:12:01 -0700, excession <excess***@discussions.microsoft.com> wrote:
> >
> > ¤ 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)
> >
Author
6 Jul 2006 3:27 PM
Paul Clement
On Wed, 5 Jul 2006 10:07:02 -0700, excession <excess***@discussions.microsoft.com> wrote:

¤ 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)
Author
6 Jul 2006 5:37 PM
excession
I'm afraid that didn't help.

Where does Word OLE header end?  78 didn't work..

Show quote
"Paul Clement" wrote:

> On Wed, 5 Jul 2006 10:07:02 -0700, excession <excess***@discussions.microsoft.com> wrote:
>
> ¤ 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)
>
Author
7 Jul 2006 3:26 PM
Paul Clement
On Thu, 6 Jul 2006 10:37:01 -0700, excession <excess***@discussions.microsoft.com> wrote:

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

AddThis Social Bookmark Button