Store is easy: use a parametrized query, and hand the byte content of the image to the database via an INSERT query:
MemoryStream ms = new MemoryStream();
myImage.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.ToArray();
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("INSERT INTO myTable (StudentId, Name, Student_img) VALUES (@ID, @NM, @IM)", con))
{
com.Parameters.AddWithValue("@ID", Id);
com.Parameters.AddWithValue("@NM", Name);
com.Parameters.AddWithValue("@IM", bytes);
com.ExecuteNonQuery();
}
}
Retrieve is no harder:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("SELECT StudentId, Name, Student_img FROM myTable", con))
{
using (SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
int id = (int) reader["StudentId"];
string desc = (string) reader["Name"];
byte[] bytes = (byte[]) reader["Student_img"];
MemoryStream ms = new MemoryStream(bytes);
myImage = Image.FromStream(ms);
}
}
}
}