我有兩個列表框。我可以通過文本框將值添加到一個列表框中。在這個列表框中,我通過單擊添加按鈕將所需的值添加到listbox2。listbox values to database
現在我無法獲得如何將第二個列表框中的值添加到名爲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]);
}
}
}
我想你需要給我們更多關於你正在使用的數據庫類型的信息。 –
如果你能告訴我們你正在使用什麼樣的數據庫,那真的很有幫助。 – womp
你的問題到底是什麼?我在你的代碼片段中沒有看到任何數據訪問代碼......你甚至嘗試過嗎? –