如何从数据库中获取值?必须声明标量变量“@ID1”

发布于 2024-12-19 09:24:08 字数 740 浏览 5 评论 0原文

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

欲拥i 2024-12-26 09:24:09

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

引用:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(commandText, connection);
    command.Parameters.Add("@ID", SqlDbType.Int);
    command.Parameters["@ID"].Value = customerID;

    // Use AddWithValue to assign Demographics.
    // SQL Server will implicitly convert strings into XML.
    command.Parameters.AddWithValue("@demographics", demoXml);

    try
    {
        connection.Open();
        Int32 rowsAffected = command.ExecuteNonQuery();
        Console.WriteLine("RowsAffected: {0}", rowsAffected);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

SqlCommand has a Parameters collection.

see also: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx

quote:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(commandText, connection);
    command.Parameters.Add("@ID", SqlDbType.Int);
    command.Parameters["@ID"].Value = customerID;

    // Use AddWithValue to assign Demographics.
    // SQL Server will implicitly convert strings into XML.
    command.Parameters.AddWithValue("@demographics", demoXml);

    try
    {
        connection.Open();
        Int32 rowsAffected = command.ExecuteNonQuery();
        Console.WriteLine("RowsAffected: {0}", rowsAffected);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
蝶…霜飞 2024-12-26 09:24:09

您需要传入参数的值。在初始化 dataCommand 变量后立即添加此行:

dataCommand.Parameters.Add("@ID", ID1);

You need to pass in the value of the parameter. Add this line right after you initialize the dataCommand variable:

dataCommand.Parameters.Add("@ID", ID1);
梦萦几度 2024-12-26 09:24:09

您需要向 DataCommand 添加一个参数,因为您说过有一个参数。

ID=@ID1

告诉服务器从参数@ID1 中获取数据。那么你就不用声明参数了。

检查 DataCommand 对象上的参数集合。添加一个参数。

You need to add a parameter to the DataCommand because you said there's one.

ID = @ID1

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.

流绪微梦 2024-12-26 09:24:09

您必须将参数添加到 SqlCommand.Parameters 集合中,因为您
您的查询中包含 @ID1

之前执行此操作

dataCommand.Parameters.Add(new SqlParameter("@ID1", ID1));

ExecuteScalar完成解决方案

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

        dataCommand.Parameters.Add(new SqlParameter("@ID1", ID1));

        cn.Open();

        var = Convert.ToString(dataCommand.ExecuteScalar());

        label3.Text = var;
    }
}

希望这有帮助

You have to add the Parameter to the SqlCommand.Parameters collection because you
have @ID1 within your query.

do that before ExecuteScalar

dataCommand.Parameters.Add(new SqlParameter("@ID1", ID1));

complete solution

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

        dataCommand.Parameters.Add(new SqlParameter("@ID1", ID1));

        cn.Open();

        var = Convert.ToString(dataCommand.ExecuteScalar());

        label3.Text = var;
    }
}

hope this helps

你是年少的欢喜 2024-12-26 09:24:08
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); 
    dataCommand.Parameters.AddWithValue("@ID1", ID1); // this is the new line of code

    cn.Open(); 
    var = Convert.ToString(dataCommand.ExecuteScalar()); 
    label3.Text = var; 

} 

参见上文

,我添加了 dataCommand.Parameters.AddWithValue("@ID1", ID1);。它的作用是定义 SQL 查询的参数并将值传递给它。我假设您希望变量 ID1 成为 SQL 参数 @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); 
    dataCommand.Parameters.AddWithValue("@ID1", ID1); // this is the new line of code

    cn.Open(); 
    var = Convert.ToString(dataCommand.ExecuteScalar()); 
    label3.Text = var; 

} 

}

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 variable ID1 to be the value of your SQL parameter @ID1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文