将图片数据存入sqlserver数据库

来源:互联网 发布:安卓电子书制作软件 编辑:程序博客网 时间:2024/05/16 08:53

关于图片的存储我们一般的做法是将图片存在硬盘上,路径存入数据库     

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;            }        }


     
0 0