如何从数据库中获取值?必须声明标量变量“@ID1”
public partial class Form3 : Form
{
string var;
int ID1;
private void button1_Click(object sender, EventArgs e)
{
ID1 = int.Parse(textBox1.Text);
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\asp\Desktop\DatabasesPractice\DatabasesPractice\soccer.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand dataCommand = new SqlCommand("SELECT Name FROM team WHERE ID = @ID1", cn);
cn.Open();
var = Convert.ToString(dataCommand.ExecuteScalar());
label3.Text = var;
}
}
它给了我一个错误,说我必须声明标量变量@ID
,我正在到处搜索,但无法找到适合我的具体情况的解决方案。
感谢人们的帮助!你的解决方案有效:D
public partial class Form3 : Form
{
string var;
int ID1;
private void button1_Click(object sender, EventArgs e)
{
ID1 = int.Parse(textBox1.Text);
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\asp\Desktop\DatabasesPractice\DatabasesPractice\soccer.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand dataCommand = new SqlCommand("SELECT Name FROM team WHERE ID = @ID1", cn);
cn.Open();
var = Convert.ToString(dataCommand.ExecuteScalar());
label3.Text = var;
}
}
It gives me an error saying I must declare the scalar variable @ID
, I'm searching everywhere and can't get a solution for my specific case.
Thanks for the help people! your solutions worked :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
http://msdn.microsoft.com/en-us /library/system.data.sqlclient.sqlcommand.aspx
SqlCommand
有一个Parameters
集合。另请参阅:http://msdn.microsoft。 com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
引用:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx
SqlCommand
has aParameters
collection.see also: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
quote:
您需要传入参数的值。在初始化 dataCommand 变量后立即添加此行:
You need to pass in the value of the parameter. Add this line right after you initialize the dataCommand variable:
您需要向 DataCommand 添加一个参数,因为您说过有一个参数。
告诉服务器从参数@ID1 中获取数据。那么你就不用声明参数了。
检查 DataCommand 对象上的参数集合。添加一个参数。
You need to add a parameter to the DataCommand because you said there's one.
tells the server to puck up the data from the parameter @ID1. Then you don't declare a parameter.
Check the Parameters collection on the DataCommand object. Add a parameter.
您必须将参数添加到
SqlCommand.Parameters
集合中,因为您您的查询中包含
@ID1
。之前执行此操作
在
ExecuteScalar
完成解决方案希望这有帮助
You have to add the Parameter to the
SqlCommand.Parameters
collection because youhave
@ID1
within your query.do that before
ExecuteScalar
complete solution
hope this helps
参见上文
,我添加了
dataCommand.Parameters.AddWithValue("@ID1", ID1);
。它的作用是定义 SQL 查询的参数并将值传递给它。我假设您希望变量ID1
成为 SQL 参数@ID1
的值。}
See above, I have added
dataCommand.Parameters.AddWithValue("@ID1", ID1);
. What that does is defines your parameter for your SQL query and passes it the value. I assumed that you'd want the variableID1
to be the value of your SQL parameter@ID1
.