【发布时间】:2021-09-06 18:02:11
【问题描述】:
早安,
在 c# 中,我尝试运行 MySQL 更新查询以根据其 id 更新一条记录。只要我不使用参数,一切都很顺利。
我在添加一个或多个参数后遇到此问题。我在这里只用一个参数和同样的问题进行了测试。
我在这里错过了什么?
非常感谢您的帮助。
public static void editCustomerTest(ClsCustomerTest pTest)
{
MySqlConnection l_Connection = null;
string l_SpName = string.Empty;
MySqlCommand l_MyCommand = null;
try
{
l_Connection = ClsIconEnv.getDataAccess().MySqlConnection;
ClsDataAccess.OpenConnection(l_Connection);
l_SpName = "update tbTestCustomers " +
"set sName = '@sLastName', " +
"sFirstName = '@sFirstName', " +
"sAddress = '@sAddress' " +
"Where id = @id);";
l_MyCommand = new MySqlCommand(l_SpName, l_Connection);
l_MyCommand.Parameters.Add("@sLastName", pTest.Last_Name);
l_MyCommand.Parameters.Add("@sFirstName", pTest.First_name);
l_MyCommand.Parameters.Add("@sAddress", pTest.Address);
l_MyCommand.Parameters.Add("@id", pTest.id);
l_MyCommand.ExecuteNonQuery(); // <----- This is the line at which the execution stops
ClsDataAccess.CloseConnection(l_Connection);
}
catch (Exception exc)
{
ClsIconErrorManager.manageException(exc);
}
finally
{
}
}
【问题讨论】:
-
去掉参数占位符周围的刻度(全部)
-
@ŇɏssaPøngjǣrdenlarp:谢谢,但不确定你的意思。你是说这个 ' 字符吗?
-
除了刻度,
MySqlParameterCollection.Add()的任何覆盖都不接受Add(ParameterName, ParameterValue的参数),您应该使用AddwithValue(),或者Add()的有效覆盖之一赋值,例如l_MyCommand.Parameters.Add("@id", MySqlDbType.Int32).Value = pTest.id; -
我认为你的意思是使用 AddWithValue