gpt4 book ai didi

c# - SQLException 必须声明标量变量 "@variableName"

转载 作者:行者123 更新时间:2023-11-30 22:29:17 25 4
gpt4 key购买 nike

我在尝试更新特定行的数据库时遇到一些问题,我收到错误消息必须声明标量变量“@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());
}
}

最佳答案

您需要将 lvl 变量添加为 SqlParamter

sqlCmdStatement.Parameters.AddWithValue("@lvl", lvl);

关于c# - SQLException 必须声明标量变量 "@variableName",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com