Hi,
Insert image to the database is also like saving other data. The following is an explame.
byte[] buffer;
string imagePath;
private void button1_Click(object sender, EventArgs e)
{
imagePath = @"C:\Users\### \Desktop\smile.gif";
FileStream filestream = new FileStream(imagePath, FileMode.Open);
buffer = new byte[filestream.Length];
filestream.Read(buffer, 0, (int)filestream.Length);
filestream.Close();
SqlConnection conn = new SqlConnection("Data Source=.;user = sa;password = sa;database = factory");
SqlCommand comm = new SqlCommand();
try
{
comm.Connection = conn;
comm.CommandText = "insert into ImageTest " + "(Name, image) values (@Name, @image)";
SqlParameter[] par = new SqlParameter[2];
par[0] = new SqlParameter("@name", "Smile");
par[1] = new SqlParameter("@image", buffer);
comm.Parameters.AddRange(par);
conn.Open();
comm.ExecuteNonQuery();
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
finally
{
conn.Close();
comm.Dispose();
conn.Dispose();
}
}
A useful link:
http://www.codeproject.com/KB/web-image/PicManager.aspx
Display image from database on the form
How To Display an Image from a Database in a Windows Forms PictureBox by Using Visual Basic .NET
http://support.microsoft.com/kb/321900/en-us
HOW TO: Copy a Picture from a Database Directly to a PictureBox Control with Visual C#
http://support.microsoft.com/kb/317701/en-us
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.