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

时间:2011-12-01 14:02:32

标签: c# database scalar

  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

5 个答案:

答案 0 :(得分:4)

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的值。

答案 1 :(得分:2)

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

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

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

  

ID = @ ID1

告诉服务器从参数@ ID1中取出数据。然后你不要声明参数。

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

答案 4 :(得分:0)

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

ExecuteScalar

之前执行此操作
dataCommand.Parameters.Add(new SqlParameter("@ID1", 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.Add(new SqlParameter("@ID1", ID1));

        cn.Open();

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

        label3.Text = var;
    }
}

希望这会有所帮助