保存图像C#windows应用程序时,空路径名称不合法 [英] Empty path name is not legal when I save images C# windows application

查看:100
本文介绍了保存图像C#windows应用程序时,空路径名称不合法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我保存所有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进入easypaisa(trid,selectopt,smobile,rmobile,snic,rnic,tamount,tcharges,totalcharges,date,descr,inv_no,Company,user_name,sprof,dataimg,image2)值(' + textBox1.Text + ',' + comboBox1.Text + ',' + textBox2.Text + ',' + textBox3.Text + ',' + textBox4。文字+ ',' + 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( 数据已成功保存 重要消息
MessageBoxButtons.OK,MessageBoxIcon.Information);

解决方案

首先,您不应该将值直接连接到SQL语句。这使您可以打开SQL注入,转换问题等。



错误本身是什么,试图找出抛出异常的位置。使用调试器并查看传递给方法的值。例如, imgloc imgloc1 包含什么。



另一件事是你应该使用块和try..catch结构来确保正确处理对象并正确处理错误。有关更多信息,请查看正确执行数据库操作 [

I want to save multiple Images now when I save all the 2 images I get no exception... but when I try to save only 1 or 2 or its throws an exception "Empty Path is not legal". my code is here

What I have tried:

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);

解决方案

First of all, you should never concatenate the values directly to the SQL statement. This leaves you open for SQL injections, conversion problems and so on.

What comes to the error itself, try to pinpoint the location where the exception is thrown. Use the debugger and have a look at the values you pass to the methods. For example what do the imgloc and imgloc1 contain.

Another thing is that you should use using blocks and try..catch structures to ensure that you properly dispose the objects and handle the errors correctly. For more information, have a look at
Properly executing database operations[^]


这篇关于保存图像C#windows应用程序时,空路径名称不合法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆