You need to enable JavaScript to run this app. C#更新数据库tblProduct表提示成功但数据未变更问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

C#更新数据库tblProduct表提示成功但数据未变更问题咨询

故障排查及修复方案

问题根因

  • 未校验更新操作实际生效行数:ExecuteNonQuery()会返回SQL执行后受影响的行数,你当前的代码无论是否匹配到要更新的产品、无论更新是否实际生效,都会直接弹出更新成功提示,这是你看到提示但数据没变化的核心原因。
  • 主键匹配逻辑错误:pcode是表的主键,属于精确匹配场景,你用了LIKE关键字进行匹配,若txtPCode.Text存在前后空格、大小写和数据库存储不一致、或者拼写错误的情况,会完全匹配不到待更新的行,导致更新失效。
  • 品牌/分类ID查询逻辑错误:查询品牌、分类ID时你先执行了dr.Read()再判断dr.HasRows,如果下拉框选中的品牌/分类不存在于对应表中,会导致bid/cid赋值为空,若tblProduct的bid/cid字段有非空约束或者外键约束,会直接导致更新语句执行失败。
  • 品牌/分类查询存在SQL注入风险:你直接拼接下拉框文本到SQL语句中,如果选项中存在单引号等特殊字符会导致SQL语法错误,也有注入风险。

修复后的代码

private void btnUpdate_Click(object sender, EventArgs e)
{
    try
    {
        if (MessageBox.Show("Are you sure to update this Product?", "Update Product", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            string bid = ""; 
            string cid = "";
            // 获取品牌ID,改为参数化查询
            cn.Close();
            cn.Open();
            cm = new SqlCommand("select id from tblBrand where brand = @brand", cn);
            cm.Parameters.AddWithValue("@brand", cboBrands.Text.Trim());
            dr = cm.ExecuteReader();
            // 先判断是否有数据再读
            if (dr.HasRows) 
            {
                dr.Read();
                bid = dr[0].ToString(); 
            }
            dr.Close();
            cn.Close();
            // 品牌不存在提前终止
            if(string.IsNullOrEmpty(bid))
            {
                MessageBox.Show("选中的品牌不存在,请重新选择", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 获取分类ID,改为参数化查询
            cn.Open();
            cm = new SqlCommand("select id from tblCategory where category = @category", cn);
            cm.Parameters.AddWithValue("@category", cboCategory.Text.Trim());
            dr = cm.ExecuteReader();
            if (dr.HasRows) 
            { 
                dr.Read();
                cid = dr[0].ToString(); 
            }
            dr.Close();
            cn.Close();
            // 分类不存在提前终止
            if(string.IsNullOrEmpty(cid))
            {
                MessageBox.Show("选中的分类不存在,请重新选择", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 更新产品,主键改为精确匹配
            cn.Open();
            cm = new SqlCommand("UPDATE tblProduct SET barcode=@barcode, pdesc=@pdesc, bid=@bid, cid=@cid, price=@price where pcode = @pcode" ,cn);
            cm.Parameters.AddWithValue("@pcode", txtPCode.Text.Trim());
            cm.Parameters.AddWithValue("@barcode", txtBarcode.Text.Trim());
            cm.Parameters.AddWithValue("@pdesc", txtDescription.Text.Trim());
            cm.Parameters.AddWithValue("@bid", bid);
            cm.Parameters.AddWithValue("@cid", cid);
            cm.Parameters.AddWithValue("@price", txtPrice.Text.Trim());
            // 校验实际更新行数
            int updateRows = cm.ExecuteNonQuery();
            cn.Close();

            if(updateRows > 0)
            {
                MessageBox.Show("Product has been successfully updated.", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                clear();
                plist.LoadProducts();
                this.Close();
            }
            else
            {
                MessageBox.Show("未找到对应产品,更新失败,请检查产品编码是否正确", "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
    catch (Exception ex)
    {
        if(cn.State == System.Data.ConnectionState.Open)
        {
            cn.Close();
        }
        MessageBox.Show(ex.Message);
    }
}

内容的提问来源于stack exchange,提问作者Shoghi Effendi Amoakwah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 15:54:10