2

我在尝试更新特定行的数据库时遇到了一些问题,我收到了错误消息Must declare scalar variable "@lvl"。不太确定我应该做什么。我需要在我的 SQL 语句中声明值吗?

private void button1_Click(object sender, EventArgs e)
{
    GridViewCellInfo grd = (GridViewCellInfo)radGridView1.Rows[0].Cells[0];
    string lvl = grd.Value.ToString(); 

    string sqlPatientCmd =
        @"UPDATE MotorTB
          SET RightColumn = @RightColumnCB, LeftColumn =  @leftColumnCB
          WHERE (Level = @lvl)";
    SqlConnection connString = new SqlConnection(@"Data Source=MERCURY\SQLEXPRESS;Initial Catalog=AsiaDB; Integrated Security=SSPI;User ID=MERCURY\Sophie;");
    try {
        connString.Open();

        SqlCommand sqlCmdStatement = new SqlCommand(sqlPatientCmd, connString);

        GridViewCellInfo grid;
        grid = (GridViewCellInfo)radGridView1.Rows[0].Cells[1];
        string rightColVal = grid.Value.ToString();

        grid = (GridViewCellInfo)radGridView1.Rows[0].Cells[2];
        string leftColVal = grid.Value.ToString();

        sqlCmdStatement.Parameters.AddWithValue("@rightColumnCB", rightColVal);
        sqlCmdStatement.Parameters.AddWithValue("@leftColumnCB", leftColVal);
        sqlCmdStatement.ExecuteScalar();
    } catch (Exception ex) {
        Console.WriteLine(ex.ToString());
    }

    // Close the connection
    try {
        connString.Close();
    } catch (Exception ex) {
        Console.WriteLine(ex.ToString());
    }
}
4

2 回答 2

2

您需要将 lvl var 添加为 SqlParamter

sqlCmdStatement.Parameters.AddWithValue("@lvl", lvl);
于 2012-04-22T14:33:02.453 回答
1

@lvl 需要作为参数提供或在查询中以其他方式定义。

于 2012-04-22T14:33:21.067 回答