【问题标题】:Mysql update query works without using parameters but doesn't when using themMysql更新查询在不使用参数的情况下工作,但在使用它们时不起作用
【发布时间】: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

标签: c# mysql


【解决方案1】:

您不需要将参数包装到字符串中,如果您不想显式指定类型,则必须使用 AddWithValue 而不是 Add,像这样

l_SpName = "update tbTestCustomers " +
"set sName = @sLastName, " +
"sFirstName = @sFirstName, " +
"sAddress = @sAddress" +
"Where id = @id);";


l_MyCommand.Parameters.AddWithValue("@sLastName", pTest.Last_Name);
l_MyCommand.Parameters.AddWithValue("@sFirstName", pTest.First_name);
l_MyCommand.Parameters.AddWithValue("@sAddress", pTest.Address);
l_MyCommand.Parameters.AddWithValue("@id", pTest.id);

【讨论】:

    【解决方案2】:

    像这样:

            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.AddWithValue("@sLastName", pTest.Last_Name);
            l_MyCommand.Parameters.AddWithValue("@sFirstName", pTest.First_name);
            l_MyCommand.Parameters.AddWithValue("@sAddress", pTest.Address);
            l_MyCommand.Parameters.AddWithValue("@id", pTest.id);
    
            l_MyCommand.ExecuteNonQuery(); 
    

    【讨论】:

    • SO 语法荧光笔不太喜欢 @string,但它是整理转储到 C# 中的 SQL 字符串的一种有效且合理的方法 - 所有 "+" 都变得有点多......
    猜你喜欢
    • 2016-03-02
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多