首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我保存图像c# windows应用程序时,空路径名不合法

当我保存图像c# windows应用程序时,空路径名不合法
EN

Stack Overflow用户
提问于 2016-11-27 00:47:18
回答 1查看 347关注 0票数 0

我想现在保存多个图像,当我保存所有的2个图像时,我得到了没有例外…但当我尝试只保存1或2或其抛出异常“空路径是不合法的”。我的代码在这里

代码语言:javascript
复制
  byte[] img = null;
  FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
  BinaryReader br = new BinaryReader(fs);
  img = br.ReadBytes((int)fs.Length);

  //

  byte[] img1 = null;
  FileStream fs1 = new FileStream(imgloc1, FileMode.Open, FileAccess.Read);
  BinaryReader br1 = new BinaryReader(fs1);
  img1 = br1.ReadBytes((int)fs1.Length);


  //
  con.Open();
  SqlCommand cmd1 = new SqlCommand("insert into easypaisa (trid,selectopt,smobile,rmobile,snic,rnic,tamount,tcharges,totalcharges,date,descr,inv_no,Company,user_name,sprof,dataimg,image2) values ('" + textBox1.Text + "','" + comboBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + dateTimePicker1.Value.ToString("MM-dd-yyyy") + "','" + textBox10.Text + "','" + textBox9.Text + "','" + comboBox2.Text + "','" + Get_User_label.Text + "','" + textBox13.Text + "',@dataimg,@image2)", con);
  cmd1.Parameters.Add(new SqlParameter("@dataimg", img));
  cmd1.Parameters.Add(new SqlParameter("@image2", img1));
  cmd1.ExecuteNonQuery();
  con.Close();

  MessageBox.Show("Data Saved successfully", "Important Message",
                   MessageBoxButtons.OK, MessageBoxIcon.Information);
EN

回答 1

Stack Overflow用户

发布于 2016-11-27 01:01:52

在读取文件之前添加文件路径验证

代码语言:javascript
复制
  byte[] img = null;
  if(!String.IsNullOrEmpty(imgloc){
     FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
     BinaryReader br = new BinaryReader(fs);
    img = br.ReadBytes((int)fs.Length);
  }


  byte[] img1 = null;
  if(!String.IsNullOrEmpty(imgloc1){
     FileStream fs1 = new FileStream(imgloc1, FileMode.Open, FileAccess.Read);
     BinaryReader br1 = new BinaryReader(fs1);
     img1 = br1.ReadBytes((int)fs1.Length);
  }

还要为图像参数设置空值,因为现在它们可以为DBNull

代码语言:javascript
复制
cmd1.Parameters.Add(new SqlParameter("@dataimg", img?? DBNull.Value));
cmd1.Parameters.Add(new SqlParameter("@image2", img1?? DBNull.Value));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40820647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档