必须声明标量变量

时间:2014-06-10 07:24:44

标签: c# sql sql-server

private void button3_Click(object sender, EventArgs e)
    {
        string ssr;
        SqlConnection scr = new SqlConnection(@"Data Source=USER-PC\MSSQL;Initial Catalog=Highscore;Integrated Security=True");
        scr.Open();
        ssr = "Select Nume,Scor,DataInitiala,DataRecenta FROM Users where DataInitiala between @Param and @Param1 ";
        SqlCommand cmd2 = new SqlCommand(ssr, scr);
        cmd2.Parameters.AddWithValue("@Param", from.Text);
        cmd2.Parameters.AddWithValue("@Param1", to.Text);
        SqlDataAdapter adapter1 = new SqlDataAdapter(ssr, scr);
        DataSet ds1 = new DataSet();
        adapter1.Fill(ds1);
        dataGridView1.DataSource = ds1.Tables[0];
        dataGridView1.Refresh();  


    }

我在这里缺少什么?我有错误必须声明标量变量@Param

2 个答案:

答案 0 :(得分:2)

实际上您在 cmd2 中声明了参数变量,但是您在 adapter1 中调用了 ssr

试试这个

private void button3_Click(object sender, EventArgs e)
    {
        string ssr;
        SqlConnection scr = new SqlConnection(@"Data Source=USER-PC\MSSQL;Initial Catalog=Highscore;Integrated Security=True");
        scr.Open();
        ssr = "Select Nume,Scor,DataInitiala,DataRecenta FROM Users where DataInitiala between @Param and @Param1 ";
        SqlCommand cmd2 = new SqlCommand(ssr, scr);
        cmd2.Parameters.AddWithValue("@Param", from.Text);
        cmd2.Parameters.AddWithValue("@Param1", to.Text);
        SqlDataAdapter adapter1 = new SqlDataAdapter();
        adapter1.SelectCommand = cmd2;
        DataSet ds1 = new DataSet();
        adapter1.Fill(ds1);
        dataGridView1.DataSource = ds1.Tables[0];
        dataGridView1.Refresh();         
    }

答案 1 :(得分:0)

请参见以下查询:

private void button3_Click(object sender, EventArgs e)
    {
        string ssr;
        SqlConnection scr = new SqlConnection(@"Data Source=USER-PC\MSSQL;Initial Catalog=Highscore;Integrated Security=True");
        scr.Open();
        ssr = "Select Nume,Scor,DataInitiala,DataRecenta FROM Users where DataInitiala between @Param and @Param1 ";
        SqlCommand cmd2 = new SqlCommand(ssr, scr);
        cmd2.Parameters.AddWithValue("@Param", from.Text);
        cmd2.Parameters.AddWithValue("@Param1", to.Text);
        SqlDataAdapter adapter1 = new SqlDataAdapter();
        adapter1.SelectCommand = cmd2;
        DataSet ds1 = new DataSet();
        adapter1.Fill(ds1);
        dataGridView1.DataSource = ds1.Tables[0];
        dataGridView1.Refresh();         
    }