ArthurSAS 2016-12-20 04:53 采纳率: 33.3%
浏览 1964

C#将图片以二进制存入数据库

public void SaveImage(string MID, OpenFileDialog openF)//将图片以二进制存入数据库中
{
string strimg = openF.FileName.ToString(); //记录图片的所在路径
FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
BinaryReader br = new BinaryReader(fs);
byte[] imgBytesIn= br.ReadBytes((int)fs.Length); //将流读入到字节数组中
conn.Open();
StringBuilder strSql = new StringBuilder();
strSql.Append("update tb_employee Set employeePhoto=@Photo where employeeID=" + MID);
SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);
cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
cmd.ExecuteNonQuery();
conn.Close();
}

图片说明

            “System.Data.SqlClient.SqlException”类型的未经处理的异常在 System.Data.dll 中发生 

其他信息: 不能将值 NULL 插入列 'employeePhoto',表 'db_PMS.dbo.tb_employee';列不允许有 Null 值。INSERT 失败。

为什么图片总会成NULL

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-12-20 05:03
    关注

    imgBytesIn是否是null,调试下。

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型