将列表框中的项目发送到数据库中的单个单元格 [英] Send item in listbox to a single cell in database
本文介绍了将列表框中的项目发送到数据库中的单个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表框,我希望将此框中的每个项目发送到单个单元格,使用逗号分隔每个项目。我对此很陌生,任何人都可以提供帮助。到目前为止我有......
I have a listbox, I want every item in this box to be sent to a single cell, using commas to separate each item. I'm quite new to this, can anyone help. so far I have...
for (int i = 0; i < CustLB.Items.Count; i++)
{
ListItem item = new ListItem();
item.Text = CustLB.Items[i].Text;
item.Value = CustLB.Items[i].Value;
OleDbCommand lct = new OleDbCommand("UPDATE [Or] SET [Loc] = (@Loc)", MAcon);
cmd.Parameters.AddWithValue("@Loc", ",");
cmd.ExecuteNonQuery();
}
我的尝试:
What I have tried:
string value = "";
for (int i = 0; i < CustLB.Items.Count; i++)
{
if (value != "")
{
OleDbCommand lct = new OleDbCommand("UPDATE [Or] SET [Loc] = (@Loc)", MAcon);
cmd.Parameters.AddWithValue("@Loc", value += ",");
}
value += CustLB.Items[i].Text;
}
// Now you have all the values in comma (,) separated string.
string[] arr = value.split(',');
推荐答案
你应该避免使用OR
作为字段名称。它的保留字 [ ^ ]在大多数数据库中!
如果你想建立逗号分隔的字符串值,请检查:
You should avoid of usingOR
as a field name. It's reserved word[^] in most databases!
If you would like to build comma separated string value, check this:
string commaVal = string.Join(",", CustLB.Items.Cast<object>()
.Select(x=> x.ToString()));
OleDbCommand lct = new OleDbCommand("UPDATE [Or] SET [Loc] = @Loc", MAcon);
cmd.Parameters.AddWithValue("@Loc", commaVal ",");
另一种方法是使用 StringBuilder [ ^ ]。
为什么?因为字符串 [ ^ ]是不可变的!
Another way is to use StringBuilder[^].
Why? Because string[^] is immutable!
//create new instance of StringBuilder
StringBuilder sb = new StringBuilder();
foreach(object o in CustLB.Items)
{
//append string
sb.Append(string.Format("{0},", o.ToString));
}
//finally
string loc = sb.ToString();
获取ListBox项的String []:
To get a String[] of ListBox Items:
string[] lbItemsAsStrArray = listBox1.Items.Cast<string>().ToArray();
我想知道你是否在这里使用ListView,而不是ListBox。
I wonder if you are using a ListView here, rather than a ListBox.
如果我正确理解了这个问题,那么你正试图在WHERE子句中构建一个IN列表。如果是这种情况,可能会像
If I understand the question correctly, you're trying to build an IN list in the WHERE clause. If this is the case, perhaps something like
OleDbCommand lct = new OleDbCommand("UPDATE [Or] SET [Loc] IN ", MAcon);
string inContent = "";
for (int i = 0; i < CustLB.Items.Count; i++) {
inContent +=
这篇关于将列表框中的项目发送到数据库中的单个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文