我有两个列表框。我可以通过文本框将值添加到一个列表框中。从此列表框中,我通过单击添加按钮将所需的值添加到listbox2。
现在我无法了解如何将第二个列表框中的值添加到名为Machines的数据库表中。这些值会添加到不同的行中。
Listbox2 PC1 PC2 PC3
并将这些添加到数据库表机器
请帮帮我
这是将值从一个列表框传递到其他...的代码...
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;
using System.IO;
namespace WebApplication3
{
public partial class WebForm2 : System.Web.UI.Page
{
ArrayList lasset = new ArrayList();
ArrayList lsubordinate = new ArrayList();
static ArrayList UpdateList = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
//add one by one//
protected void Button4_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (!lasset.Contains(ListBox1.Items[i]))
{
lasset.Add(ListBox1.Items[i]);
}
}
}
for (int i = 0; i < lasset.Count; i++)
{
if (!ListBox2.Items.Contains(((ListItem)lasset[i])))
{
ListBox2.Items.Add(((ListItem)lasset[i]));
}
ListBox1.Items.Remove(((ListItem)lasset[i]));
}
}
}
//Add all
protected void Button5_Click(object sender, EventArgs e)
{
while (ListBox1.Items.Count != 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (!lasset.Contains(ListBox1.Items[i]))
{
lasset.Add(ListBox1.Items[i]);
}
}
for (int i = 0; i < lasset.Count; i++)
{
if (!ListBox2.Items.Contains(((ListItem)lasset[i])))
{
ListBox2.Items.Add(((ListItem)lasset[i]));
}
ListBox1.Items.Remove(((ListItem)lasset[i]));
}
}
}
//remove from listbox2 and add to listbox1//
protected void Button6_Click(object sender, EventArgs e)
{
if (ListBox2.SelectedItem != null)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
if (!lsubordinate.Contains(ListBox2.Items[i]))
{
lsubordinate.Add(ListBox2.Items[i]);
}
}
}
for (int i = 0; i < lsubordinate.Count; i++)
{
if (!ListBox1.Items.Contains(((ListItem)lsubordinate[i])))
{
ListBox1.Items.Add(((ListItem)lsubordinate[i]));
}
ListBox2.Items.Remove(((ListItem)lsubordinate[i]));
UpdateList.Add(lsubordinate[i]);
}
}
}
//remove all
protected void Button7_Click(object sender, EventArgs e)
{
while (ListBox2.Items.Count != 0)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (!lsubordinate.Contains(ListBox2.Items[i]))
{
lsubordinate.Add(ListBox2.Items[i]);
}
}
for (int i = 0; i < lsubordinate.Count; i++)
{
if (!ListBox1.Items.Contains(((ListItem)lsubordinate[i])))
{
ListBox1.Items.Add(((ListItem)lsubordinate[i]));
}
ListBox2.Items.Remove(((ListItem)lsubordinate[i]));
UpdateList.Add(lsubordinate[i]);
}
}
}
答案 0 :(得分:1)
您在评论中提供了此代码。
myConn.Open();
OleDbCommand dataCommand = new OleDbCommand();
if (ListBox2.Items.Count > 0) {
foreach (ListItem i in ListBox2.Items) {
insertContractCmd = ("insert into table column1) values ('"
+ ListBox2.Items
+ "')", myConn);
}
}
myConn.Open();
dataCommand.ExecuteNonQuery();
}
我发现代码有三个问题:
首先,insert命令中的括号不匹配。将("insert into table column1)
更改为"insert into table (column1)
其次,您打开连接两次。丢失第二个myConn.Open();
第三,这是最大的问题 - 您正在尝试将字符串与整个项目列表连接起来。你应该做的是分别连接每个项目,如下所示:
// note: this is a bad example, use the next one instead
insertContractCmd = "insert into table (column1) values (";
foreach (ListItem item in ListBox2.Items) {
insertContractCmd = insertContractCmd + "'" + item.Text + "'";
}
insertContractCmd = insertContractCmd + ")";
现在你只需处理一个小问题 - 这段代码很糟糕,因为它会导致很多字符串连接效率低下并占用大量内存。您应该使用StringBuilder
代替:
StringBuilder commandBuilder = new StringBuilder("insert into table (column1) values (");
foreach (ListItem item in ListBox2.Items) {
commandBuilder.AppendFormat("'{0}'", item.Text);
}
commandBuilder.Append(")");
insertContractCmd = commandBuilder.ToString();
注意:出于同样的效率原因,我也使用了AppendFormat。您也可以使用commandBuilder.Append("'" + item.Text + "'");
来达到同样的效果。
希望有所帮助,configurator
。