【发布时间】:2020-05-31 15:54:17
【问题描述】:
这是我的代码,它显示成功,但它没有更新表中的行。
cm = new SqlCommand("update table_products set pname = @pname, pdesc = @pdesc, bid = @bid, cid = @cid, cost = @cost, price = @price, reorder = @reorder where pcode like @pcode", con);
cm.Parameters.AddWithValue("@pcode", tft_pcode.Text);
cm.Parameters.AddWithValue("@barcode", tft_barcode.Text);
cm.Parameters.AddWithValue("@pname", tft_pname.Text);
cm.Parameters.AddWithValue("@pdesc", tft_pdescription.Text);
cm.Parameters.AddWithValue("@bid", bid);
cm.Parameters.AddWithValue("@cid", cid);
cm.Parameters.AddWithValue("@cost", Double.Parse(tft_cost.Text));
cm.Parameters.AddWithValue("@price", Double.Parse(tft_price.Text));
cm.Parameters.AddWithValue("@reorder", int.Parse(tft_reorder.Text));
cm.ExecuteNonQuery();
con.Close();
MessageBox.Show("Product updated successfully");
【问题讨论】:
-
您应该查看Can we stop using AddWithValue() already? 并停止使用
.AddWithValue()- 它可能会导致意想不到和令人惊讶的结果... -
您的
tft_pcode.Text值是什么样的?当您执行SELECT * FROM dbo.table_products WHERE pcode LIKE (that value)时,您会得到任何结果吗?? -
@marc_s 虽然我更喜欢 AddWithValue 而不是 SQL 可注入性!
-
@CaiusJard:同意!
标签: c# sql sql-server sql-update xquery-update