使用C#实现MySQL中StockInTbl库存数量累加至ProductTbl产品数量
问题解决:MySQL库存入库后更新产品库存的C#实现
核心问题梳理
- 原Update语句错误:语法不符合MySQL规范,关联表逻辑错误(不该关联ProductTbl自身,需关联StockInTbl与ProductTbl),还存在字符串拼接导致的语法问题和SQL注入风险。
- 逻辑位置确定:库存累加逻辑必须放在StockIn按钮点击事件中——只有当新入库记录添加完成后,才需要同步更新对应产品的库存数量。
- 额外问题修正:原代码使用的
SqlCommand是SQL Server专用类,操作MySQL需改用MySqlCommand;同时必须放弃字符串拼接SQL的写法,改用参数化查询避免安全隐患。
修正后的SQL更新语句
MySQL中直接用本次入库数量累加产品现有库存的简洁写法:
UPDATE ProductTbl SET ProductQuantity = ProductQuantity + @StockQty WHERE ProductID = @ProductID;
如果需要基于刚插入的入库记录做关联更新(确保数据一致性),可使用JOIN语法:
UPDATE ProductTbl tb1 JOIN StockInTbl tb2 ON tb1.ProductID = tb2.ProductID -- 需确保StockInTbl有ProductID关联字段 SET tb1.ProductQuantity = tb1.ProductQuantity + tb2.StockQuantity WHERE tb2.BatchID = @BatchID;
注:原入库表StockInTbl似乎缺少与ProductTbl关联的ProductID字段,需补充该字段或调整逻辑,否则无法定位到要更新的产品。
完整修正后的StockIn按钮事件代码
private void button1_Click_1(object sender, EventArgs e) { // 输入校验 if (string.IsNullOrEmpty(BatchIdTb.Text) || string.IsNullOrEmpty(SupplierNameTb.Text) || string.IsNullOrEmpty(VarietyCombo.SelectedValue?.ToString()) || string.IsNullOrEmpty(StockQty.Text)) { MessageBox.Show("请填写所有必填字段"); return; } // 转换库存数量为整数 if (!int.TryParse(StockQty.Text, out int stockInQty)) { MessageBox.Show("库存数量必须是有效整数"); return; } // 需根据实际场景获取当前入库对应的产品ID(比如从下拉框或专门的输入框) string productId = "替换为实际获取的ProductID"; using (var con = new MySqlConnection("你的MySQL连接字符串")) { con.Open(); // 开启事务,确保入库插入与库存更新原子性 using (var transaction = con.BeginTransaction()) { try { // 1. 插入入库记录 string insertStockSql = @"INSERT INTO StockInTbl (BatchID, SupplierName, VarietyID, StockQuantity, StockInDate) VALUES (@BatchID, @SupplierName, @VarietyID, @StockQty, @StockInDate)"; using (var insertCmd = new MySqlCommand(insertStockSql, con, transaction)) { insertCmd.Parameters.AddWithValue("@BatchID", BatchIdTb.Text); insertCmd.Parameters.AddWithValue("@SupplierName", SupplierNameTb.Text); insertCmd.Parameters.AddWithValue("@VarietyID", VarietyCombo.SelectedValue.ToString()); insertCmd.Parameters.AddWithValue("@StockQty", stockInQty); insertCmd.Parameters.AddWithValue("@StockInDate", StockInDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); insertCmd.ExecuteNonQuery(); } // 2. 更新产品库存 string updateProductSql = @"UPDATE ProductTbl SET ProductQuantity = ProductQuantity + @StockQty WHERE ProductID = @ProductID"; using (var updateCmd = new MySqlCommand(updateProductSql, con, transaction)) { updateCmd.Parameters.AddWithValue("@StockQty", stockInQty); updateCmd.Parameters.AddWithValue("@ProductID", productId); int affectedRows = updateCmd.ExecuteNonQuery(); if (affectedRows == 0) { MessageBox.Show("未找到对应产品,库存更新失败"); transaction.Rollback(); return; } } // 提交事务 transaction.Commit(); MessageBox.Show("库存入库成功,产品库存已更新"); populate(); } catch (Exception ex) { transaction.Rollback(); MessageBox.Show($"操作失败:{ex.Message}"); } } } }
关键优化点说明
- 参数化查询:彻底杜绝SQL注入风险,同时解决字符串类型转换的语法问题。
- 事务处理:保证入库记录插入与库存更新要么同时成功,要么同时回滚,避免数据不一致。
- 输入校验:完善类型校验与空值检查,提升代码健壮性。
- 适配MySQL驱动:使用
MySqlConnection和MySqlCommand替代SQL Server专用类,确保与MySQL兼容。
内容的提问来源于stack exchange,提问作者John
相关产品推荐
相关产品推荐

