【发布时间】:2018-02-11 17:03:26
【问题描述】:
我有一个表单,它使用 SQL 查询将数据从 listview 传输到 listbox。我没有错误,SQL 代码正在读取查询并将数据从listview 传输到listbox 正在工作,但似乎我的SqlCommand 没有运行/更新我的datagridview。我不知道这是否适合放置SqlCommand - 请帮我解决这个问题。
for (int intCount = 0; intCount < listViewOrders.Items.Count; intCount++)
{
listBoxInvoice.Items.Add(listViewOrders.Items[intCount].Text);
listBoxInvoice.Items.Add(" x" + listViewOrders.Items[intCount].SubItems[2].Text +
" @ " + listViewOrders.Items[intCount].SubItems[1].Text);
connect.Open();
SqlCommand cmd = new SqlCommand("UPDATE tblProducts SET productQuantity = productQuantity - " +
listViewOrders.Items[intCount].SubItems[2].Text + "WHERE productName = '" + listViewOrders.Items[intCount].Text + "'", connect);
connect.Close();
}
dataGridProd.Update();
dataGridProd.Refresh();
提前谢谢你。欢迎任何类型的回复。
【问题讨论】:
-
你在运行时有什么错误吗?
-
@JericCruz 到目前为止没有错误,但它没有更新 productQuantity
-
SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看Little Bobby Tables
-
你只是定义
cmd- 但你永远不会执行(运行)它.....你需要使用cmd.ExecuteNonQuery()(在connect.Close()调用之前)实际运行该代码 .....
标签: c# sql-server winforms listview listbox