Retrieveing binary data out of the database

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 74
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training

Retrieveing binary data out of the database

 
0
  #1
Nov 5th, 2009
1. How to retrieve stored file our of the database?

have stored (for example) a pdf file into database, as a data type of varbinary(MAX) - binary data. How can I get this file back, like it was before?



Important things I have stored for the particular file are:

- ID

- File name

- File type (pdf, word, ... document)

- and of course the content (which is now in binary data)



2. One more small thing:

I would like to get the ID from a Method1 which retrieves the ID, into another Method2 (in the same class), where I will use this ID as a variable.

I have this code for examle:

  1. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. int IDDocument; //How to get into IDDocument an ID value from the GetID method (from int Doc_ID)
  4. GetID(IDDocument); //I got an error here: Use of unassigned local variable 'IDDocument
  5. string SQL = "SELECT Content FROM Documents WHERE DocumentID = '" + IDDocoment + "'";
  6. //...
  7. }
  8.  
  9. private void GetID(int Doc_ID)
  10. {
  11. string DocID = "SELECT DocumentID FROM Documents WHERE Name = '" + listBox1.SelectedItem + "'";
  12. SqlCommand cmd = new SqlCommand(DocID, sqlConn);
  13. cmd.CommandType = CommandType.Text;
  14. sqlConn.Open();
  15. SqlDataReader reader = cmd.ExecuteReader();
  16. if (reader.Read())
  17. {
  18. Doc_ID = reader.GetInt32(0);
  19. }
  20. reader.Close();
  21. sqlConn.Close();
  22. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,460
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #2
Nov 5th, 2009
PDF, XLS, DOC, DOCX, it doesn't matter. The image is stored as binary on the SQL server. You just need to pull down the byte[] array and store it to disk with the proper file extension. Here is an example:
  1. //Download image
  2. private void button2_Click(object sender, EventArgs e)
  3. {
  4.  
  5. const string connStr = "Data Source=apex2006sql;Initial Catalog=ServManLeather;Integrated Security=True;";
  6. const string query = "Select ImgNumber, FacilityImg From FacilityImg Where ImgNumber = 1000";
  7. using (SqlConnection conn = new SqlConnection(connStr))
  8. {
  9. conn.Open();
  10. using (SqlCommand cmd = new SqlCommand(query, conn))
  11. {
  12. using (SqlDataReader dr = cmd.ExecuteReader())
  13. {
  14. using (DataTable dt = new DataTable())
  15. {
  16. dt.Load(dr);
  17. //File.WriteAllBytes(@"C:\picture.bmp", (byte[])dt.Rows[0]["FacilityImg"]);
  18. using (MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["FacilityImg"]))
  19. {
  20. ms.Position = 0;
  21. ms.Seek(0, SeekOrigin.Begin);
  22. pictureBox1.Image = System.Drawing.Image.FromStream(ms);
  23. ms.Flush();
  24. ms.Close();
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }

I notice in your code you are not using using() blocks or calling .Dispose() on classes that implement IDisposable . You should read up on that to prevent leaking unmanaged resources

For you next question you need to use an out parameter to indicate that the value will be set in the called method. Example:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. int IDDocument;
  4. GetID(out IDDocument);
  5. MessageBox.Show(IDDocument.ToString());
  6. }
  7.  
  8.  
  9. private void GetID(out int Doc_ID)
  10. {
  11. Doc_ID = 2;
  12. }
Last edited by sknake; Nov 5th, 2009 at 4:48 pm.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 74
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training
 
0
  #3
Nov 5th, 2009
Originally Posted by sknake View Post
For you next question you need to use an out parameter to indicate that the value will be set in the called method. Example:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. int IDDocument;
  4. GetID(out IDDocument);
  5. MessageBox.Show(IDDocument.ToString());
  6. }
  7.  
  8.  
  9. private void GetID(out int Doc_ID)
  10. {
  11. Doc_ID = 2;
  12. }

Ok, but I do not have just Doc_ID = 2; in my GetID method.
I have this:
  1. string DocID = "SELECT DocumentID FROM Documents WHERE Name = '" + listBox1.SelectedItem + "'";
  2. SqlCommand cmd = new SqlCommand(DocID, sqlConn);
  3. cmd.CommandType = CommandType.Text;
  4. sqlConn.Open();
  5. SqlDataReader reader = cmd.ExecuteReader();
  6. if (reader.Read())
  7. {
  8. Doc_ID = reader.GetInt32(0);
  9. }
  10. reader.Close();
  11. sqlConn.Close();

In my case I got an error:
The out parameter 'Doc_ID' must be assigned to before control leaves the current method.
Last edited by Mitja Bonca; Nov 5th, 2009 at 6:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,460
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
Nov 6th, 2009
So fix the error....

  1. string DocID = "SELECT DocumentID FROM Documents WHERE Name = '" + listBox1.SelectedItem + "'";
  2. SqlCommand cmd = new SqlCommand(DocID, sqlConn);
  3. cmd.CommandType = CommandType.Text;
  4. sqlConn.Open();
  5. SqlDataReader reader = cmd.ExecuteReader();
  6. if (reader.Read())
  7. {
  8. Doc_ID = reader.GetInt32(0);
  9. }
  10. else
  11. {
  12. Doc_ID = -1;
  13. }
  14. reader.Close();
  15. sqlConn.Close();
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 74
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training
 
0
  #5
Nov 6th, 2009
Thank you, will remember for the next time.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 412 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC