Here is my code of saving image-
private void button1_Click(object sender, EventArgs e)
{
MemoryStream ms1 = new MemoryStream();
pictureBox1.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] img_arr1 = new byte[ms1.Length];
ms1.Read(img_arr1, 0, img_arr1.Length);
SqlCommand cmd = new SqlCommand("insert into stud(name,age,attach)values(@a,@b,@c)", con);
cmd.Parameters.AddWithValue("@a", label4.Text);
cmd.Parameters.AddWithValue("@b", label5.Text);
cmd.Parameters.AddWithValue("@c", img_arr1);
cmd.ExecuteNonQuery();
MessageBox.Show("Data inserted successfully");
con.Close();
}
The fetching code is as follows-
SqlCommand cmd = new SqlCommand("select attach from stud where name=@a", con);
cmd.Parameters.AddWithValue("a", textBox1.Text);
try
{
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
byte[] img_arr1 = (byte[])dr["attach"];
MemoryStream ms1 = new MemoryStream(img_arr1);
ms1.Seek(0, SeekOrigin.Begin);
pictureBox1.Image = Image.FromStream(ms1);
}
else
{
MessageBox.Show("Your Data is not inserted in database try again");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
For saving image in table I used 'image' as a datatype
Khan Abrar AhmedPosted Oct 15, 2014, 7:28 AM
cmd.Parameters.AddWithValue("a", textBox1.Text);
u have to add "@"
cmd.Parameters.AddWithValue("@a", textBox1.Text);
Thamilselvan JagadeesanPosted Oct 15, 2014, 12:51 PM
Please refer the below article explaning how to store and retrieve the image from sql server, so that you will get idea
http://www.dotnetgallery.com/kb/resource21-How-to-store-and-retrieve-images-from-SQL-server-database-using-aspnet.aspx
Thanks
Munesh SharmaPosted Oct 15, 2014, 7:30 AM
http://dotnet-munesh.blogspot.in/2014/05/how-to-save-image-in-database-in-window.html
Munesh SharmaPosted Oct 15, 2014, 7:19 AM