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