943,991 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1218
  • C# RSS
Nov 5th, 2009
0

Retrieveing binary data out of the database

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 5th, 2009
1
Re: Retrieveing binary data out of the database
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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 5th, 2009
0
Re: Retrieveing binary data out of the database
Click to Expand / Collapse  Quote originally posted by sknake ...
For you next question you need to use an out parameter to indicate that the value will be set in the called method. Example:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 6th, 2009
0
Re: Retrieveing binary data out of the database
So fix the error....

C# Syntax (Toggle Plain Text)
  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();
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 6th, 2009
0
Re: Retrieveing binary data out of the database
Thank you, will remember for the next time.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Enable Macros
Next Thread in C# Forum Timeline: C# Multi-form pre-render?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC