本文介绍了在两个列表框之间交换项目在sql表中所做的更改为.......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="LstLeft" runat="server" SelectionMode="multiple" Width="200px" Rows="5"></asp:ListBox>
<asp:Button ID="BtnRight" runat="server" Text=">" onclick="BtnRight_Click"/>
<asp:Button ID="BtnRightAll" runat="server" Text=">>" onclick="BtnRightAll_Click"/>
<asp:Button ID="BtnLeftAll" runat="server" Text="<<" onclick="BtnLeftAll_Click"/>
<asp:Button ID="BtnLeft" runat="server" Text="<" onclick="BtnLeft_Click"/>
<asp:ListBox ID="LstRight" runat="server" SelectionMode="multiple" Width="200px" Rows="5"></asp:ListBox>
</div>
</form>
</body>
</html>
cs_page中的代码
code in cs_page
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class rough : System.Web.UI.Page
{
public static ArrayList Files = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Left_List();
Right_List();
}
}
protected void Left_List()
{
try
{
string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
SqlConnection con = new SqlConnection(str);
string com = "Select username from Ttable";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataSet myDataSet = new DataSet();
adpt.Fill(myDataSet, "Ttable");
DataTable myDataTable = myDataSet.Tables[0];
DataRow tempRow = null;
foreach (DataRow tempRow_Variable in myDataTable.Rows)
{
tempRow = tempRow_Variable;
LstLeft.Items.Add((tempRow["username"] + " "));
}
}
catch
{
throw;
}
}
protected void Right_List()
{
try
{
string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
SqlConnection con = new SqlConnection(str);
string com = "Select city from dTable";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataSet myDataSet = new DataSet();
adpt.Fill(myDataSet, "dTable");
DataTable myDataTable = myDataSet.Tables[0];
DataRow tempRow = null;
foreach (DataRow tempRow_Variable in myDataTable.Rows)
{
tempRow = tempRow_Variable;
LstRight.Items.Add((tempRow["city"] + " "));
}
}
catch
{
throw;
}
}
protected void BtnRight_Click(object sender, EventArgs e)
{
for (int i = LstLeft.Items.Count - 1; i >= 0; i--)
{
if (LstLeft.Items[i].Selected)
{
LstRight.Items.Add(LstLeft.Items[i]);
LstRight.ClearSelection();
LstLeft.Items.Remove(LstLeft.Items[i]);
}
}
string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
SqlConnection con = new SqlConnection(str);
SqlCommand cmd_check = con.CreateCommand();
cmd_check.CommandType = CommandType.Text;
SqlCommand cmd_insert = con.CreateCommand();
cmd_insert.CommandType = CommandType.Text;
try
{
con.Open();
}
catch (SqlException ex)
{
}
//Inserts into dTable and deltes from TTable according to the final RIGHT list
for (int i = 0; i < LstRight.Items.Count; i++)
{
cmd_check.CommandText = "select count(*) from dTable where city='" + LstRight.Items[i] + "'";
if ((int)cmd_check.ExecuteScalar() == 0)
{
cmd_insert.CommandText = "insert into dTable values('" + LstRight.Items[i] + "')";
cmd_insert.ExecuteNonQuery();
}
}
for (int i = 0; i < LstRight.Items.Count; i++)
{
cmd_check.CommandText = "select * from Ttable where username='" + LstRight.Items[i] + "'";
if (cmd_check.ExecuteNonQuery() != 0)
{
}
}
}
protected void BtnRightAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < LstLeft.Items.Count; i++)
{
LstRight.Items.Add(LstLeft.Items[i]);
}
LstLeft.Items.Clear();
string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
SqlConnection con = new SqlConnection(str);
SqlCommand cmd_check = con.CreateCommand();
cmd_check.CommandType = CommandType.Text;
SqlCommand cmd_insert = con.CreateCommand();
cmd_insert.CommandType = CommandType.Text;
try
{
con.Open();
}
catch (SqlException ex)
{
}
//Inserts into dTable and deltes from TTable according to the final RIGHT list
for (int i = 0; i < LstRight.Items.Count; i++)
{
cmd_check.CommandText = "select count(*) from dTable where city='" + LstRight.Items[i] + "'";
if ((int)cmd_check.ExecuteScalar() == 0)
{
cmd_insert.CommandText = "insert into dTable values('" + LstRight.Items[i] + "')";
cmd_insert.ExecuteNonQuery();
}
}
for (int i = 0; i < LstRight.Items.Count; i++)
{
cmd_check.CommandText = "select * from Ttable where username='" + LstRight.Items[i] + "'";
if (cmd_check.ExecuteNonQuery() != 0)
{
}
}
}
protected void BtnLeft_Click(object sender, EventArgs e)
{
for (int i = LstRight.Items.Count - 1; i >= 0; i--)
{
if (LstRight.Items[i].Selected)
{
LstLeft.Items.Add(LstRight.Items[i]);
LstLeft.ClearSelection();
LstRight.Items.Remove(LstRight.Items[i]);
}
}
string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
SqlConnection con = new SqlConnection(str);
SqlCommand cmd_check = con.CreateCommand();
cmd_check.CommandType = CommandType.Text;
SqlCommand cmd_insert = con.CreateCommand();
cmd_insert.CommandType = CommandType.Text;
try
{
con.Open();
}
catch (SqlException ex)
{
}
for (int i = 0; i < LstLeft.Items.Count; i++)
{
cmd_check.CommandText = "select count(*) from TTable where username='" + LstLeft.Items[i] + "'";
if ((int)cmd_check.ExecuteScalar() == 0)
{
cmd_insert.CommandText = "insert into TTable values('" + LstLeft.Items[i] + "')";
cmd_insert.ExecuteNonQuery();
}
}
for (int i = 0; i < LstLeft.Items.Count; i++)
{
cmd_check.CommandText = "select * from dTable where city='" + LstLeft.Items[i] + "'";
if (cmd_check.ExecuteNonQuery() != 0)
{
cmd_insert.CommandText = "delete from dTable where city='" + LstLeft.Items[i] + "'";
cmd_insert.ExecuteNonQuery();
}
}
}
protected void BtnLeftAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < LstRight.Items.Count; i++)
{
LstLeft.Items.Add(LstRight.Items[i]);
}
LstRight.Items.Clear();
string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
SqlConnection con = new SqlConnection(str);
SqlCommand cmd_check = con.CreateCommand();
cmd_check.CommandType = CommandType.Text;
SqlCommand cmd_insert = con.CreateCommand();
cmd_insert.CommandType = CommandType.Text;
try
{
con.Open();
}
catch (SqlException ex)
{
}
for (int i = 0; i < LstLeft.Items.Count; i++)
{
cmd_check.CommandText = "select count(*) from TTable where username='" + LstLeft.Items[i] + "'";
if ((int)cmd_check.ExecuteScalar() == 0)
{
cmd_insert.CommandText = "insert into TTable values('" + LstLeft.Items[i] + "')";
cmd_insert.ExecuteNonQuery();
}
}
for (int i = 0; i < LstLeft.Items.Count; i++)
{
cmd_check.CommandText = "select * from dTable where city='" + LstLeft.Items[i] + "'";
if (cmd_check.ExecuteNonQuery() != 0)
{
cmd_insert.CommandText = "delete from dTable where city='" + LstLeft.Items[i] + "'";
cmd_insert.ExecuteNonQuery();
}
}
}
}
推荐答案
这篇关于在两个列表框之间交换项目在sql表中所做的更改为.......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!