您的位置:首页 > 博客中心 > 数据库 >

数据库添加图片路径异常

时间:2022-03-14 18:40

工具: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;//注意要有@ 

结果:

技术分享

 

技术分享

热门排行

今日推荐

热门手游