必须声明标量变量

本文关键字:变量 标量 声明 | 更新日期: 2023-09-27 18:07:48

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

必须声明标量变量

实际上你在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();         
    }

请参阅以下查询:

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