You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL UPDATE语句无运行报错但未更新数据,疑WHERE子句异常

问题分析与解决:UPDATE语句无报错但未更新数据

核心问题

你的WHERE子句存在写法错误:将参数@vendor_name包裹在单引号内的'%@vendor_name%'中,这会让MySQL把@vendor_name当作字面字符串处理,而非你传入的参数值,导致没有匹配到任何目标行,因此数据库数据未发生变化。

修复方案

  1. 修正模糊匹配的参数写法
    若必须使用LIKE模糊匹配,需将通配符与参数分离,正确写法为:

    WHERE vendor_name LIKE CONCAT('%', @vendor_name, '%')
    

    或者在传入参数值时拼接通配符(但前者更符合参数化查询的规范)。

  2. 优先使用唯一主键作为更新条件
    用vendor_name做模糊匹配更新风险极高,可能误改多条符合条件的记录。建议改用表中的唯一主键(如vendor_id)作为WHERE条件,确保仅更新目标行。

  3. 检查受影响行数
    ExecuteNonQuery()会返回受影响的行数,通过判断该值可直接确认是否有记录被更新,便于排查问题。

  4. 移除无意义的异常处理
    当前catch块仅重新抛出异常,未做任何额外处理,可直接删除,让异常自然向上传递。

修改后的示例代码(使用唯一主键)

using (MySqlConnection con = new MySqlConnection(ConfigurationManager.AppSettings["RL_InventoryConnection"]))
{
    if (con.State == ConnectionState.Closed)
        con.Open();

    string updateSql = @"UPDATE vendormaster 
                       SET vendor_name = @vendor_name, 
                           vendor_contact_Name1 = @vendor_contact_Name1, 
                           vendor_contact_phone1 = @vendor_contact_phone1, 
                           vendor_contact_email1 = @vendor_contact_email1, 
                           vendor_contact_Name2 = @vendor_contact_Name2, 
                           vendor_contact_phone2 = @vendor_contact_phone2, 
                           vendor_contact_email2 = @vendor_contact_email2, 
                           vendor_pan = @vendor_pan, 
                           vendor_gst = @vendor_gst, 
                           vendor_address = @vendor_address, 
                           vendor_pincode = @vendor_pincode, 
                           vendor_city = @vendor_city, 
                           vendor_country = @vendor_country 
                       WHERE vendor_id = @vendor_id;";

    MySqlCommand cmd = new MySqlCommand(updateSql, con);
    // 新增唯一主键参数,确保精准更新
    cmd.Parameters.AddWithValue("@vendor_id", getVendorId);
    cmd.Parameters.AddWithValue("@vendor_name", getvendorname);
    cmd.Parameters.AddWithValue("@vendor_contact_Name1", getcontactperson1);
    cmd.Parameters.AddWithValue("@vendor_contact_phone1", getcontactphone1);
    cmd.Parameters.AddWithValue("@vendor_contact_email1", getcontactemail1);
    cmd.Parameters.AddWithValue("@vendor_contact_Name2", getcontactperson2);
    cmd.Parameters.AddWithValue("@vendor_contact_phone2", getcontactphone2);
    cmd.Parameters.AddWithValue("@vendor_contact_email2", getpersonemail2);
    cmd.Parameters.AddWithValue("@vendor_pan", getvendorPan);
    cmd.Parameters.AddWithValue("@vendor_gst", getvendorgst);
    cmd.Parameters.AddWithValue("@vendor_address", getvendoraddress);
    cmd.Parameters.AddWithValue("@vendor_pincode", getpincode);
    cmd.Parameters.AddWithValue("@vendor_city", getcity);
    cmd.Parameters.AddWithValue("@vendor_country", getcountry);

    int affectedRows = cmd.ExecuteNonQuery();
    if (affectedRows == 0)
    {
        // 可根据需求记录日志或抛出自定义异常
        throw new InvalidOperationException("未找到匹配的供应商记录,更新操作未生效");
    }
}

若坚持使用模糊匹配的修正写法

将WHERE子句替换为:

WHERE vendor_name LIKE CONCAT('%', @vendor_name, '%')

内容的提问来源于stack exchange,提问作者Mohd Sameer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 21:50:28