How to save an Image in SqlServer Database using C# dot net
In this article I explained about How to save an Image in SqlServer Database using C# dot net
.Net being the platform for distributed application now, Asp.Net can be used to store images that are small to be stored in a database like SQL SERVER 2000. For the purpose SQL Server provides a datatype called "image" which is used to store images in the database.
//create a byte[] for the image file that is uploaded
int imagelen = Upload.PostefFile.ContentLength;
byte[] picbyte = new byte[imagelen];
Upload.PostedFile.InputStream.Read(picbyte,0,imagelen);
//Inserting the image to the database
SqlConnection con = new SqlConnection("Give the connection here");
try
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Image Table "
+ "(ImageField,ImageID)Values(@pic,@imageid)",con);
cmd.Parameters.Add("@pic",picbyte);
cmd.Parameters.Add("@ImageId",lblImageID.Text);
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}