system.data.dll附加信息中发生了'system.data.sqlclient.sqlexception'类型的未处理异常:必须声明标量变量“@ name”。 [英] An unhandled exception of type 'system.data.sqlclient.sqlexception' occurred in system.data.dll additional information: must declare the scalar variable "@name".

查看:244
本文介绍了system.data.dll附加信息中发生了'system.data.sqlclient.sqlexception'类型的未处理异常:必须声明标量变量“@ name”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  button2_Click(对象发​​件人,EventArgs e)
{
字符串 insert = 插入[dbo]。[Student_Info]值(@ Name,@ Address,@ FatherName,@ Class);
SqlCommand cmd = new SqlCommand(insert,con);
SqlParameter [] prm = new SqlParameter [ 4 ];
prm [ 0 ] = new SqlParameter( @ Name,SqlDbType.VarChar);
prm [ 0 ]。Value = textBox9.Text;

prm [ 1 ] = new SqlParameter( @ Address,SqlDbType.VarChar);
prm [ 1 ]。Value = textBox8.Text;

prm [ 2 ] = new SqlParameter( @ FatherName,SqlDbType.VarChar);
prm [ 2 ]。Value = textBox7.Text;
prm [ 3 ] = new SqlParameter( @ Class,SqlDbType.VarChar);
prm [ 3 ]。Value = textBox6.Text;

con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show( Inserted Data);
}





我的尝试:



请帮帮我,我有这个需要声明标量变量的错误

解决方案

你没有将参数添加到命令中:

  //  准备参数数组 

cmd.Parameters.AddRange(prm);

// 执行命令



或者你可以直接在集合中添加参数:

 cmd.Parameters.Add(  @ Name,SqlDbType.VarChar).Value = textBox9.Text; 


For如果没有任何列说明符的INSERT语句,SQL会假定每列都有一个值,并且所有值都按表中列的确切顺序提供。使用这种方法是不好的做法。如果你更改了表格的模式,如果没有正确更新,你就会破坏所有代码。



你应该像
那样进行插入

 INSERT INTO dbo.Student_Info(Name,Address,FatherName,Class)
VALUES(@ Name,@ Address,@ EmployName,@ Class)



请注意,我猜测你的列名,只是假设它们与参数值的名称相匹配。


你只是在创建SqlParameter对象,您没有将它们附加到命令。创建它们之后,将它们附加到命令的参数集合



 cmd.Parameters.Add(prm [ 0 ]); 


private void button2_Click(object sender, EventArgs e)
{
   string insert ="insert into [dbo].[Student_Info] values (@Name,@Address,@FatherName,@Class)";
   SqlCommand cmd = new SqlCommand(insert, con);
   SqlParameter[] prm = new SqlParameter[4];
   prm[0] = new SqlParameter("@Name", SqlDbType.VarChar);
   prm[0].Value = textBox9.Text;

   prm[1] = new SqlParameter("@Address", SqlDbType.VarChar);
   prm[1].Value = textBox8.Text;

   prm[2] = new SqlParameter("@FatherName", SqlDbType.VarChar);
   prm[2].Value = textBox7.Text;
   prm[3] = new SqlParameter("@Class", SqlDbType.VarChar);
   prm[3].Value = textBox6.Text;

   con.Open();
   cmd.ExecuteNonQuery();
   con.Close();
   MessageBox.Show("Inserted Data");
}



What I have tried:

please help me i have this erroe that want must be declare scalar variable

解决方案

You didn't add the parameters to the command:

// prepare parameter array

cmd.Parameters.AddRange(prm);

// execute command


Alternatively you can add parameters directly to the collection:

cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = textBox9.Text;


For an INSERT statement without any column specifiers, SQL assumes that every column will have a value and all values are supplied in the exact order of the columns in the table. Using this method is bad practice. If you change the schema of the table, you break all of your code if it isn't updated properly.

You should be doing your inserts like

INSERT INTO dbo.Student_Info (Name, Address, FatherName, Class)
VALUES (@Name, @Address, @FatherName, @Class)


Note that I'm guessing at your column names and just assuming they match the names of your parameter values.


You're simply creating SqlParameter objects, you're not attaching them to the command. After you create them attach them to the command's parameters collection

cmd.Parameters.Add(prm[0]);


这篇关于system.data.dll附加信息中发生了'system.data.sqlclient.sqlexception'类型的未处理异常:必须声明标量变量“@ name”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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