将信息从列表框传输到sql [英] transfer information from listbox to sql

查看:46
本文介绍了将信息从列表框传输到sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我想要几行数据从列表框传输到sql
请帮助我

hello
I want Several rows of data transfer from listbox to sql
please help me

推荐答案

事实上,我的列表框包含多个项目,希望它们包含在表中的几列中"

那就是越来越难的地方.
如果要将列表框中的一项分配给每一行,那么这很容易-只需循环并执行适当的INSERT语句即可.

但是要输入到列中的可变数量的项目会比较困难,因为每列都必须同时存在,并且必须在数据库中命名,然后才能插入值.
如果表中有5列,而ListBox中有6个条目,该怎么办?还是5列和4个条目?哪一个错过了?

从代码的角度来看,要对列表框/列的特定组合进行处理并不复杂,但是要在更一般的情况下进行处理则要困难得多.

您想在这里实现什么?为什么要用这种方式设计数据库?



",例如:cod = 1,名称=比萨,count = 2和price = 8
"in fact i have list box contain several items and want them to several column in the table"

That is where it gets harder.
If you are assigning one item from your ListBox to each Row, then that is easy - just loop through and execute the appropriate INSERT statement.

But a variable number of items being entered into columns is harder, as each column must both exist, and be named in the database before you can insert the values.
What do you do if you have 5 columns in your table, and 6 entries in the ListBox? Or 5 columns, and 4 entries? Which one gets missed out?

It''s not complicated from a code point of view to do something that works for a specific combination of List box/columns, but to do it for a more general case is considerably harder.

What are you trying to achieve here? Why have you designed your database this way?



"for example :cod=1 ,name=pizza ,count=2 and price=8



鳕鱼= 2,名称=苏打水,计数= 2和价格
我想在表格的4列中进行1click转移"


好,所以您的列表框中有很多行,其中包含用逗号分隔的项目.
我将首先获取列表框的内容:

cod=2,name=soda ,count=2 and price
i want 1click transfer in the 4column in table"


OK, So you have a number of rows in your list box, which contain items separated by commas.
I would start by getting the list box contents, one by one:
foreach (object o in myListBox.Items)
    {
    string s = o as string;
    if (s != null)
        {
        ...
        }
    }

下一个问题是将字符串分成各个字段.如何执行此操作取决于您对将它们放在首位的代码的信任程度如何::laugh:
最简单的方法是将字符串分成多个部分,每个部分用逗号分隔.那应该给您和包含"xxx = yyy"的数组,因此您需要对其进行处理以找出每个部分应该去的地方.或者,如果您仅屏蔽列表框以包含有效信息,则可以再次将其拆分:

The next problem is to break the string into the various fields. How you do this depends on how much you trust the code that put them in there in the first place... :laugh:
The easiest way is to split the string into the various parts, one for each section separated by comma. That should give you and array containing "xxx=yyy", so you need to process that to work out where each part should go. Or, if you truyst the list box only to contain valid information, you could just split them again:

string[] parts = s.Split(',');
string code = parts[0].Split('=')[1].Trim();
string name = parts[1].Split('=')[1].Trim();
string count = parts[2].Split('=')[1].Trim();
string price = parts[3].Split('=')[1].Trim();


就我个人而言,我不信任任何代码-即使我编写了它也不会(它可能会在以后更改,所以我不会这样做!)我不会这样做的方法是不要在其中添加字符串第一名-我将使用带有ToString覆盖的类代替)
根据数据库中的字段类型,您可能必须进行一些转换为整数,十进制或其他形式的转换.

然后可以将其插入数据库.
在方法开始时,您将需要一个SqlConnection:


Personally, I don''t trust any code - not even if I wrote it (it may get changed later, so I wouldn''t do it like this! The way I would do it is not to put a string in in the first place - I would use a class with a ToString override instead)
You may have to do some conversions to integer, decimal, or whatever, depending on the field types in your database.

Then you can insert it to the database.
At the start of your method you will need an SqlConnection:

SqlConnection con = new SqlConnection(sonnectionString);
con.Open();

您应该已经拥有的连接字符串.
然后在循环中:

The connection string you should already have.
Then in the loop:

using (SqlCommand cmd = new SqlCommand("INSERT INTO MyTable (code, name, count, price) VALUES (@COD, @NAM, @CNT, @PRI)", con))
    {
    cmd.Parameters.AddWithValue("@COD", code);
    cmd.Parameters.AddWithValue("@NAM", name);
    cmd.Parameters.AddWithValue("@CNT", count);
    cmd.Parameters.AddWithValue("@PRI", price);
    cmd.ExecuteNonQuery();
    }

完成!


使用此代码:
Use this code:
if (ListBox1.Items.Count > 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    string selectedItem = ListBox1.Items[i].Text;
                    //insert command
                }
            }
        }


请参阅此链接:
http://forums.asp.net/t/917044.aspx/1 [ ^ ]
http://forums.devx.com/showthread.php?t=169294 [ ^ ]


Refer this links:
http://forums.asp.net/t/917044.aspx/1[^]
http://forums.devx.com/showthread.php?t=169294[^]


这篇关于将信息从列表框传输到sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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