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

write byte trouble

Krantor
P: 4
Hi all,

i am quite new to c# and was wondering if i could get assistance on an issue im having with writing a binary file from the database to my local machine, this is the code i have:

Expand|Select|Wrap|Line Numbers
  1. string inputString = Convert.ToString(reader["Attachment"]);
  2.  
  3.                         byte[] AttachmentByte;
  4.                         AttachmentByte = StringToByteArray(inputString);
  5.  
  6.                         string dir = "";
  7.                         dir = reader["Attachment"].ToString();
  8.                         string FilePath = dir;
  9.  
  10.                         // Specify a "currently active folder"
  11.                         string activeDir = @"C:\Documents and Settings";
  12.                         //Create a new subfolder under the current active folder
  13.                         string newPath = System.IO.Path.Combine(activeDir, "PDF Attachments");
  14.                         // Create a new file name.
  15.                         string newFileName = "Attachments.pdf";
  16.                         // Combine the new file name with the path
  17.                         newPath = System.IO.Path.Combine(activeDir, newFileName);
  18.                         // Create the file and write to it.
  19.                         // REMEMBER: System.IO.File.Create will overwrite the file if it already exists.
  20.                         //if (!System.IO.File.Exists(newPath))
  21.                         //{
  22.                             using (System.IO.FileStream fs = System.IO.File.Create(newPath))
  23.                             {
  24.                                 for (byte i = 0; i < 100; i++)
  25.                                 {
  26.                                     fs.Write(AttachmentByte, 0, AttachmentByte.Length);
  27.                                 }
  28.                             }
and the function for converting the string to a byte:

Expand|Select|Wrap|Line Numbers
  1. public static byte[] StringToByteArray(string inputString)
  2.     {
  3.         System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  4.         return encoding.GetBytes(inputString);
  5.     }
the problem is that the file is 230kb, but when i write it the i only get 2kb.

is there something i am doing wrong?
Dec 11 '08 #1
Share this Question
Share on Google+
1 Reply


nukefusion
Expert 100+
P: 221
If I was going to store a binary file within a database field I would probably open the reader in SequentialAccess mode, use the SqlDataReader.GetBytes() method to retrieve the binary data into a buffer and a BinaryWriter object to write it down to the file stream. I've done this a couple of times before, albeit some time ago.

You may have better results going down that route as when attempting to convert binary to strings and back again you can come across all sorts of encoding issues.

You also seem to have something weird going on there with your FOR loop - the iteration seems unneccesary. I've cobbled together a rough code sample to give you an idea of what I mean, although be warned I haven't had time to test it, however it should give you an idea of how to go about it.

Expand|Select|Wrap|Line Numbers
  1. SqlCommand command = new SqlCommand("SELECT Attachment FROM MyDBTable WHERE ID = 1", myConnection);
  2. SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
  3. while (reader.Read())
  4. {
  5.     FileStream fileStream = new FileStream("myBinaryFile.dat", FileMode.OpenOrCreate, FileAccess.Write);
  6.     BinaryWriter binaryWriter = new BinaryWriter(fileStream);
  7.     long startIndex = 0;
  8.     short bufferSize = 256;
  9.     long bytesLeft = bufferSize;
  10.     short bytesToRead = bufferSize;
  11.     byte[] outputBuffer = new byte[bufferSize];
  12.  
  13.     // Write bytes into the buffer
  14.     do
  15.     {
  16.         // Read the next set of bytes
  17.         bytesLeft = reader.GetBytes(reader.GetOrdinal("Attachment"), startIndex, outputBuffer, 0, bytesToRead);
  18.         // Write what we've got to the file
  19.         binaryWriter.Write(outputBuffer);
  20.         binaryWriter.Flush();
  21.         // Increment the marker position in the database field
  22.         startIndex += bytesToRead;
  23.         // Work out how many bytes to read next iteration
  24.         bytesToRead = (short)Math.Min(bytesLeft, bufferSize);
  25.  
  26.     } while (bytesLeft > 0);
  27.  
  28.     binaryWriter.Close();
  29.     fileStream.Close();
  30. }
Dec 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.