Saving a steam to disk

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hi,
I have put together a script in C# that takes a text file, then streams
it into a SQLServer database.
What I am trying to do now is create another script that does the exact
opposite: selects a file in the database, extracts it then saves it to a
disk file.

This is pretty straightforward to do in VBScript, however I'm having
problems getting it to work in C#. Can anyone help?

Cheers,
Mark
 
What is the problem doing it with .NET (C#)?

As in VBScript you can use ADO to access the DB, get the file,
and flush it to the disk file.

If your file is large enough, you may ask for data in chunks....
 
First, what kind of stream do you have the data in? If you want to
write your data to the file system, you want a FileStream.

FileStream someFileStream = File.Open("yourFileName.someExtension",
File.Create);

Will create your file, then take the array of bytes from the stream
(assuming you have a memory stream this is how you do it).

byte[] data = memStream.ToArray();
someFileStream.write(data, 0, data.Length);

someFileStream.close();

There are some other tricks to learn, like using File.Exists before
creating a file, but that's the basics of writing a stream to the file
system. Does this help?
 
Mark,

See the SqlDataReader.GetBytes method. It will give you a byte array that
you can then save to disk.

Dave
 
Back
Top