使用listview项更新多SQL表

本文关键字:SQL 更新 listview 使用 | 更新日期: 2023-09-27 18:18:05

我有一个包含itemcode - itemcount - tablename的SQL表1我的Windows form1包含listview1和button1。

当我点击button1时,列中的所有listview1 SubItems1值应该从SubItems[2]值中的表名更新,其中itemcode值是SubItems[0]。

我尝试了以下代码,但没有像它应该的那样工作,因为它只做了它与第一行有关的事情,而不是listview1中的其余行:

foreach (ListViewItem item in listView1.Items)
{
    item.Selected = true;
}
if (cn.State == ConnectionState.Closed) cn.Open();
cm = new SqlCommand();
cm.Connection = cn;
ListViewItem lvi1 = listView1.SelectedItems[0];
string tableName = lvi1.SubItems[2].Text;
string itemcode1 = lvi1.SubItems[0].Text;
string itemcode2 = lvi1.SubItems[1].Text;
string sql = "UPDATE " + tableName + " SET [itemcount]= " + itemcode2 + " WHERE [itemcode]= " + itemcode1 + " AND [itemcount] IS NOT NULL";
cm.CommandText = sql;
cm.ExecuteNonQuery();

以下是截图:

我的3个表是相同的当用户单击"保存"按钮时,它减去蓝色突出显示的列中的所有值,其中值已经在他们的表中,黑色突出显示的列中itemcode =红色突出显示的列,如下所示:

listview1和它的值

1-代码为1的项目存在于testtbl1中,如黑色高亮列所示,其计数为50:

testtbl  :  itemcode   itemcount   tablename
               1           50      testtbl1

[输入图片描述][3]

2-代码为2的项目存在于testtbl2中,如黑色高亮列所示,其计数为40:

testtbl2   itemcode   itemcount   tablename
              2            40      testtbl2

[输入图片描述][4]

3-现在如第一张照片所示,itemcode: 1的计数为15,itemcode: 2的计数为20

现在当用户单击保存按钮时,它应该减去每个项目表中存在的项目中蓝色突出显示的每个项目的项目计数,以给出以下结果:

[result in testtbl1][5]

testtbl    itemcode   itemcount   tablename
              1           35      testtbl1

[result in testtbl2][6]

testtbl2   itemcode   itemcount   tablename
              2            20      testtbl2

使用listview项更新多SQL表

我仍然觉得你的一些问题令人困惑,但我认为这是接近你想要的。我认为你想要对每一行运行更新,但你只对第一个选定行做更新。如果你想更新每一行,那么你根本不需要为SelectedRows操心,只需要遍历它们。你说你想减去计数,但你的查询没有这样做,我把它放进去,我希望这是你想要的。从我所读到的,SubItems从第二列开始(listview索引1),所以我已经调整了子项访问。如果我弄错了,重新调整一下。最后,如果允许用户在ListView中输入自由格式的文本,那么这段代码就存在SQL注入的危险,因此请确保在将值插入Update语句之前对数据进行清理。

string sql = "UPDATE {0} SET [itemcount] = [itemcount] - {1} WHERE [itemcode] = {2} AND [itemcount] IS NOT NULL";
foreach (ListViewItem item in listView1.Items)
{
    string qry = string.Format(sql, item.SubItems[1].Text, item.SubItems[0].Text, item.Text);
    using(cm = new SqlCommand(qry, cn))
    {
       cn.Open();
       cm.ExecuteNonQuery();
       cn.Close();
    }
}
<标题>跟进:

要使用参数,将SQL更改为:

string sql = "UPDATE {0} SET [itemcount] = [itemcount] - @itemcount WHERE [itemcode] = @itemcode AND [itemcount] IS NOT NULL";

并将SqlParameters添加到命令中(假设为SqlDbTypes):

SqlParameter par_ItemCount = new SqlParameter("@itemcount", SqlDbType.Int);
par_ItemCount.Value = int.Parse(item.SubItems[0].Text);
cm.Parameters.Add(par_ItemCount);
SqlParameter par_ItemCode = new SqlParameter("@itemcode", SqlDbType.Int);
par_ItemCode.Value = int.Parse(item.Text);
cm.Parameters.Add(par_ItemCode);

但是,表名仍然存在问题。不能对表名使用参数。如果用户可以通过任何可能的方式编辑表名,那么必须在将其注入SQL之前对其进行验证。这很简单,只需对照有效的表名列表进行检查即可。

foreach (ListViewItem itemRow in taskShowListView.Items){

            if (itemRow.Selected == true)
            {
                int taskId = Convert.ToInt32(itemRow.SubItems[0].Text);
                string taskDate = itemRow.SubItems[1].ToString();
                string taskDescription = itemRow.SubItems[2].ToString();
                MessageBox.Show("selected");
                ListViewItem listViewItem = new ListViewItem(Convert.ToInt32(taskId[0].Text));
                listViewItem.SubItems.Add(**taskDate[1]**.ToString());
                listViewItem.SubItems.Add(taskDescription[2].ToString());
                taskShowListView.Items.Add(listViewItem);

            }
        }