数据库添加图片路径异常

工具:SQLServer2012    Visual Studio 2013

情况:在pictureBox控件中添加照片 然后在保存在数据库中 

        我这样写的

        出现错误是:

  ps:(数据库中有image的类型,因存放的照片要是太多的话,会增加数据库的负担,因此保存图片的路径)

解决办法:

原因:是因为pictureBox1.Imagelocation是null 的

      修改代码:

if (pictureBox1.Image != null)
{
if (pictureBox1.ImageLocation == null)
{
paras.Add(new SqlParameter("@照片", DBNull.Value));
}

else {
paras.Add(new SqlParameter("@照片",pictureBox1.ImageLocation));
}
}
else
{
MessageBox.Show("照片不能为空","提示");
}

 同时还应该将所选文件的路径保存下来

   

this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
pictureBox1.ImageLocation = @openFileDialog1.FileName;//注意要有@ 

结果:

原文地址:https://www.cnblogs.com/2714585551summer/p/5280597.html