Check this code sample
protected void Button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("C:\\Files\\draft.jpg",FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ContestConnection"].ConnectionString);
SqlCommand addpic = new SqlCommand( "INSERT INTO userTable ("+" img_data) "+" VALUES(@img)",myConnection);
addpic.Parameters.Add("@img",SqlDbType.Image, photo.Length).Value = photo;
myConnection.Open();
addpic.ExecuteNonQuery();
myConnection.Close();
}
If you use FileUploadControl , then the code will be something like this
protected void btnUpload_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(ImageUploadToSQL.PostedFile.FileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] image = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BlobsConnectionString"].ConnectionString);
SqlCommand addimage = new SqlCommand("INSERT INTO Images (" + " Image, ImageName)" + " VALUES(@Image, @ImageName)", myConnection);
addimage.Parameters.Add("@Image", SqlDbType.Image, image.Length).Value = image;
addimage.Parameters.Add("@ImageName", SqlDbType.NVarChar).Value = txtFileName.Text;
myConnection.Open();
addimage.ExecuteNonQuery();
myConnection.Close();
}