Save
Sql= "INSERT into P_ry_photo (Id,photo) VALUES ('" +id+ "', @i)";
SqlCommand cmd=new SqlCommand (sql,commonvar.sqlcondatabase);
MemoryStream s = new MemoryStream ();
PicPhoto.Image.Save (S,system.drawing.imaging.imageformat.jpeg);
Byte[] B = S.toarray ();
S.close ();
Cmd. Parameters.Add ("@i", Sqldbtype.image, (int) b.length);
Cmd. parameters["@i"]. Value=b;
Cmd. ExecuteNonQuery ();
Reading images
private void Readphoto (string id)
{
Try
{
String ls_sql= "Select photo from P_ry_photo where id= '" +id+ "'";
This.picphoto.image=null;
This.picPhoto.Refresh ();
SqlCommand cmd=new SqlCommand (ls_sql,commonvar.sqlcondatabase);
SqlDataReader Reader=cmd. ExecuteReader ();
while (reader. Read ())
{
Byte[] B = (byte[]) reader[0];
MemoryStream s = new MemoryStream (b);
Bitmap bmp = new Bitmap (s);
System.Drawing.Image Image = BMP;
Picphoto.image = Image;
S.close ();
}
Reader. Close ();
}
catch (Exception ex)
{
MessageBox.Show (ex. Message);
}
}
It is recommended to save the uploaded image as a file and then store the path in the database. Database read and write pictures take too long, it is not cost-effective.
How to: C # in WinForm How to save image in SQL2000 and remove display