protected void Button1_Click(objectsender, EventArgs e)
{if (FileUpload1.PostedFile != null && !string.IsNullOrEmpty(FileUpload1.PostedFile.FileName))
{byte[] Image = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile img=FileUpload1.PostedFile;
img.InputStream.Read(Image,0, (int)FileUpload1.PostedFile.ContentLength);
SqlConnection conn= new SqlConnection(WebConfigurationManager.ConnectionStrings["DemosDatabaseConnectionString"].ConnectionString);
SqlCommand cmd= new SqlCommand("INSERT INTO SaveImageByBinary([Image],ImageName) VALUES (@ImgBin,@ImageName)", conn);
cmd.CommandType=CommandType.Text;
cmd.Parameters.Add("@ImgBin", SqlDbType.Image, Image.Length).Value =Image;
cmd.Parameters.Add("@ImageName", SqlDbType.NVarChar).Value =FileUpload1.FileName;using(conn)
{//open the connection
conn.Open();//send the sql query to store the data
cmd.ExecuteNonQuery();
Response.Write("Succeed");
}
}else{byte[] Image = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile img=FileUpload1.PostedFile;
img.InputStream.Read(Image,0, (int)FileUpload1.PostedFile.ContentLength);
SqlConnection conn= new SqlConnection(WebConfigurationManager.ConnectionStrings["DemosDatabaseConnectionString"].ConnectionString);
SqlCommand cmd= new SqlCommand("INSERT INTO SaveImageByBinary([Image],ImageName) VALUES (@ImgBin,@ImageName)", conn);
cmd.CommandType=CommandType.Text;
cmd.Parameters.Add("@ImgBin", SqlDbType.Image, Image.Length).Value =Image;
cmd.Parameters.Add("@ImageName", SqlDbType.NVarChar).Value =FileUpload1.FileName;using(conn)
{//open the connection
conn.Open();//send the sql query to store the data
cmd.ExecuteNonQuery();
Response.Write("Succeed");
}
}
}