我在尝试更新特定行的数据库时遇到了一些问题,我收到了错误消息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());
}
}