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();
}


Related Articles

Object Serialisation in dot net

This article provides an overview of the serialization used in Microsoft .NET and the usage of the same.For example, serialization is used to save session state in ASP.NET and to copy objects to the clipboard in Windows Forms. It is also used by remoting to pass objects by value from one application domain to another.

More articles: Dot Net

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: