我想现在保存多个图像,当我保存所有的2个图像时,我得到了没有例外…但当我尝试只保存1或2或其抛出异常“空路径是不合法的”。我的代码在这里
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);发布于 2016-11-27 01:01:52
在读取文件之前添加文件路径验证
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
cmd1.Parameters.Add(new SqlParameter("@dataimg", img?? DBNull.Value));
cmd1.Parameters.Add(new SqlParameter("@image2", img1?? DBNull.Value));https://stackoverflow.com/questions/40820647
复制相似问题