使用listview项目进行多sql表更新 [英] multi sql table update with listview items

查看:81
本文介绍了使用listview项目进行多sql表更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有sql表1,其中包含itemcode - itemcount - tablename

和我的windows form1包含listview1和button1



我是什么我试图做的是当我点击button1时列中的所有listview1 SubItems [1]值从SubItems [2]值中的表名更新,其中itemcode值为SubItems [0]





i尝试了下面的代码,但它做了它应该做的但是第一行只有其余部分:



i have sql table 1 that contains itemcode - itemcount - tablename
and my windows form1 contains listview1 and button1

what i'm trying to do is that when i click on button1 all listview1 SubItems[1] values in the column to update from tables names in SubItems[2] values where itemcode values are SubItems[0]


i tried the following code but it did what it should do but for the first row only not the rest:

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();



推荐答案

我建​​议您使用SQL参数而不是构建SQL字符串,但是......按照要求:



I would recommend you use SQL parameter instead of building an SQL string but...As requested:

if (cn.State == ConnectionState.Closed) cn.Open();
 
cm = new SqlCommand();
 
cm.Connection = cn;

foreach (ListViewItem item in listView1.Items)
{
   string sql = "UPDATE " + item.SubItems[2].Text + " SET [itemcount]= " + item.SubItems[1].Text + " WHERE [itemcode]= " + item.SubItems[0].Text + " AND [itemcount] IS NOT NULL";
 
   cm.CommandText = sql;
   cm.ExecuteNonQuery();
} 


这篇关于使用listview项目进行多sql表更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆