图片上传到数据库
protected void btnAdd_Click(object sender, EventArgs e)
{
string connstr = "data source=连接到计算机;initial catalog=student;User Id=sa;password=123";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto = UpPhoto.PostedFile;
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);
cmd.CommandText = "insert into boke (FIamge) values(@FImage)";
cmd.Parameters.Add("@FImage", SqlDbType.Image);
cmd.Parameters["@FImage"].Value = PhotoArray;
cmd.ExecuteNonQuery();
}
}
}