简体   繁体   English

必须声明标量变量“@img”。 C#

[英]Must declare the scalar variable "@img". c#

I am making an application which uses Picture and Thumb impression by using picture box and I am getting this error for my code below any help?我正在制作一个通过使用图片框使用图片和拇指印象的应用程序,我在下面的代码中收到此错误有什么帮助吗?

con = new SqlConnection(DBHelper.Conectionstring());
con.Open();
cmd = new SqlCommand();
cmd.Parameters.Add("@img",SqlDbType.Image);
cmd.Parameters.Add("@thmb", SqlDbType.Image);
cmd.Connection = con;
MemoryStream ms = new MemoryStream();
MemoryStream ms1 = new MemoryStream();
this.pbxaddpic.Image.Save(ms, this.pbxaddpic.Image.RawFormat);
this.pbxthumb.Image.Save(ms, this.pbxthumb.Image.RawFormat);
byte[] b = ms.GetBuffer();
byte[] t = ms.GetBuffer();
ms.Close();
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@img", b);
cmd.Parameters.AddWithValue("@thmb", t);
int maxpartycode = getmaxpartycode();
cmd.CommandText = "insert into tbl_partyinfo values(" + maxpartycode + ",'" + this.txtname.Text
      + "','" + this.txtfathername.Text + "','" + this.txtcnic.Text +"','"+this.txtaddress.Text+ "','"+this.txtcontactno.Text
      + "','" + this.txtbusinessname.Text + "','" + this.txtbusinessaddress.Text +"','"+this.txtemail.Text+"', '"+this.txtfax.Text
      +"',@img,@thmb)";
cmd.ExecuteNonQuery();
con.Close();
cmd = null;
MessageBox.Show("Details Added")

When you do (after ms.Close()):当你这样做时(在 ms.Close() 之后):

cmd.Parameters.Clear(); 

it will clear all the parameters from the Parameters collection.它将清除参数集合中的所有参数。 So when you try to add the values, the parameter is unavailable hence the error.因此,当您尝试添加值时,该参数不可用,因此会出现错误。 I don't get why you need to clear the parameters, but if you really need to do that, you will have to add parameter before adding the value.我不明白为什么需要清除参数,但如果确实需要这样做,则必须在添加值之前添加参数。 Like喜欢

  cmd.Parameters.Add("@img", SqlDbType.Image);
  cmd.Parameters.Add("@thmb", SqlDbType.Image)
  cmd.Parameters.AddWithValue("@img", b);
  cmd.Parameters.AddWithValue("@thmb", t);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM