By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,639 Members | 1,988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,639 IT Pros & Developers. It's quick & easy.

Extracting binary file from sql database image field to disk

P: n/a
I am trying to extract a zip file in a database image field to disk. For some
reason, the zip file is getting corrupted / truncated. I have code in ASP
which extracts the zip file no problem, so i know it is not corrupted in the
table. Any help would be appreciated.

FileStream fs = new FileStream(sFilePath + "TestFile.zip",
FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
byte[] buffer =
Encoding.ASCII.GetBytes(DataReader[sField].ToString());
bw.Write(buffer);
bw.Close();
fs.Close();

Aug 2 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Scott <Sc***@discussions.microsoft.comwrote:
I am trying to extract a zip file in a database image field to disk. For some
reason, the zip file is getting corrupted / truncated. I have code in ASP
which extracts the zip file no problem, so i know it is not corrupted in the
table. Any help would be appreciated.

FileStream fs = new FileStream(sFilePath + "TestFile.zip",
FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
byte[] buffer =
Encoding.ASCII.GetBytes(DataReader[sField].ToString());
bw.Write(buffer);
bw.Close();
fs.Close();
Don't convert the binary data to a string - that's a recipe for a
disaster.

If you look at DataReader[sField].GetType(), what does it say?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 2 '07 #2

P: n/a
I use the binaryformatter class for this type of thing --- MSDN example for
the binaryformatter class has a pretty good example.

Take a look at:
http://msdn2.microsoft.com/en-us/lib...formatter.aspx
Aug 2 '07 #3

P: n/a
Matt F <mf****************@nospam.nospamwrote:
I use the binaryformatter class for this type of thing --- MSDN example for
the binaryformatter class has a pretty good example.
It's a zip file - natural binary data to start with. Serializing it
would add an extra layer for no good reason, *and* make it
hard/impossible to use from other platforms.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '07 #4

P: n/a


"Jon Skeet [C# MVP]" wrote:
Scott <Sc***@discussions.microsoft.comwrote:
I am trying to extract a zip file in a database image field to disk. For some
reason, the zip file is getting corrupted / truncated. I have code in ASP
which extracts the zip file no problem, so i know it is not corrupted in the
table. Any help would be appreciated.

FileStream fs = new FileStream(sFilePath + "TestFile.zip",
FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
byte[] buffer =
Encoding.ASCII.GetBytes(DataReader[sField].ToString());
bw.Write(buffer);
bw.Close();
fs.Close();

Don't convert the binary data to a string - that's a recipe for a
disaster.

If you look at DataReader[sField].GetType(), what does it say?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
The DataReader[sField].GetType() comes back as System.String
Aug 3 '07 #5

P: n/a
Scott <Sc***@discussions.microsoft.comwrote:
If you look at DataReader[sField].GetType(), what does it say?
The DataReader[sField].GetType() comes back as System.String
Okay, that's a bad start. What's the data type in the database, and how
have you stored the zip file in there in the first place? Storing a
binary block of data (such as a zip file) in a text field is a really
bad move...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 5 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.