FileStream fImageFile = new FileStream("ImagePath", FileMode.Open);
byte[] fImageByte = new byte[(int)fImageFile.Length];//把图片转成二进制流
fImageFile.Read(fImageByte, 0, fImageByte.Length);
using (SqlConnection conn = new SqlConnection("ConnectionString"))
{
SqlCommand comm = new SqlCommand("Insert Into Table1 (Image) Values (@Image)", conn);
comm.Parameters.Add(new SqlParameter(@Image, fImageByte));
comm.Parameters.Add("@Image", SqlDbType.Binary);//@Image是sql语句里面的参数,使用时可以根据情况传参。
comm.Parameters["@Image"] = fImageByte;//存入数据库
conn.Open();
comm.EndExecuteNonQuery();
} |