可以存图片、文件等等
- 先把数据库中的表字段类型改一下
- 然后大概流程就是读取文件,写入二进制,保存至数据库。
传了三个值,数据库的连接字符串,文件路径和要改数据的ID
public void importPic(string connstr, string path, string id)
{
FileStream fs = new FileStream(path, FileMode.Open);
byte[] imagebytes = new byte[fs.Length];
fs.Read(imagebytes, 0, (int)fs.Length);
fs.Close();
SqlConnection con = new SqlConnection(connstr);
//控制大小
if (imagebytes.Count() < 409600)
{
con.Open();
//修改,也可以插入,注意要加@
string str = " update cangku set pic=@data where id=‘" + id + "’“;
SqlCommand cmd = new SqlCommand(str, con);
//将二进制流插入数据库中
cmd.Parameters.AddWithValue(”@data", imagebytes);
cmd.ExecuteNonQuery();
con.Close();
}
}