Home  »  Code Snippets  »  ASP.Net  »

Insert Image into Sql Server 2005

 Posted By : Manoranjan SahooPosted Date : 11/11/2010 11:32:48 AM Category : ASP.Net Points : 5 (Rs 1.00)


create table with below fields :
id int
img Image
Description varchar(200)
width varchar(10)
height varchar(10)

Now use below code to insert image



if (FileUpload1.PostedFile!=null && FileUpload1.PostedFile.FileName != "")
{
    byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
    HttpPostedFile Image = FileUpload1.PostedFile;
    Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
    SqlConnection myConnection = new SqlConnection("Put your Connection string");
    SqlCommand storeimage = new SqlCommand("insert into  ImageTable (img,description,width,height) values(" + "@image,'Description',@imagesize,@imageheight)", myConnection);
    storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;
    System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = img.Width;
    storeimage.Parameters.Add("@imageheight", SqlDbType.BigInt, 99999).Value = img.Height;
    myConnection.Open();
    storeimage.ExecuteNonQuery();
    myConnection.Close();
}



Thank You.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.