关于图片的存储我们一般的做法是将图片存在硬盘上,路径存入数据库
sqlServer数据库以image格式存储图片,sqlServer数据库无法直接将image格式的字段转为string类型或其他类型,可以通过间接先将image格式转为bytes格式,再转为其他类型。
图片读取及入库
FileStream fs = new FileStream(barcCodeFile, FileMode.Open);
byte[] imageBytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
public bool InsertBarCode(string ID, byte[] fs)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update t_MaterielInfo set ");
strSql.Append("BarCode_Position=@BarCode");
strSql.Append(" where ID="+ID);
SqlParameter[] parameters = {
new SqlParameter("@ID",SqlDbType.VarChar,255),
new SqlParameter("@BarCode", SqlDbType.Image)};
parameters[0].Value = ID;
parameters[1].Value = fs;
int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
if (rows > 0)
{
return true;
}
else
{
return false;
}
}