In this post i will show you How you can retrieve saved images from a SQL Database and show them in a picture box.
If you don't know how to save images into the database then you can read my recent post that i've made "Saving Images in a Database."
Click here for more details.
I am using C# as programming language and Sql Server as a back end.
Now that we have images in our database, We can easily retrieve those images and show them in a picture box control in the GUI Application.
So, Here is the Table schema that we're going to use in this exercise.
You can use the previous application that we created while storing images into the database or choose a new application.
If you're creating a new application, then drag a Picturebox, Button and textbox to your form.
Name the Button control as btnShow and textbox as txtID.
Double click the btnShow control to generate the default event handler for this.
and write the following code into it.
SqlConnection con = new SqlConnection("data source=param;initial catalog=post;Integrated Security=True");
SqlCommand command = new SqlCommand("SELECT EmpPhoto FROM Employee WHERE EmpID = @EID", con);
command.Parameters.AddWithValue("@EID", txtID.Text);
SqlDataAdapter adap = new SqlDataAdapter();
adap.SelectCommand = command;
con.Open();
Byte[] storedImage =null;
DataSet ds = new DataSet();
adap.Fill(ds, "Employee");
if (ds.Tables[0].Rows[0].IsNull("EmpPhoto"))
{
MessageBox.Show("Image Not Found");
}
else
{
storedImage = (byte[])(ds.Tables[0].Rows[0][0]);
con.Close();
Image newImage;
using (MemoryStream ms = new MemoryStream(storedImage))
{
newImage = Image.FromStream(ms);
}
pictureBox1.Image = newImage;
}
Now, we're done with coding part.
Run the application.
Form will open to you and insert the id of the employee whose image you want to retrieve and click show button.
Saturday, October 3, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment