绑定代码: 新手不是很懂 ,有没参考网站 int id = 15002; string constr = ConfigurationManager.AppSettings["cn"].ToString(); SqlConnection con = new SqlConnection(constr); con.Open(); string sqlstr = "select bumen from yz where yid = '"+id+"'"; SqlCommand cmd = new SqlCommand(sqlstr, con); SqlDataReader dr = cmd.ExecuteReader(); //mydata.Fill(ds, sqlstr); ListBox1.DataSource = dr ; ListBox1.DataTextField = "bumen"; ListBox1.DataValueField = "bumen"; ListBox1.DataBind(); con.Close(); SqlConnection con1 = new SqlConnection(constr); con1.Open(); string sqlstr1 = "select username from yh where id <='10000' and id not in (select bid from yz where yid = '"+id+"')"; SqlCommand cmd1 = new SqlCommand(sqlstr1, con1); SqlDataReader dr1 = cmd1.ExecuteReader(); //mydata.Fill(ds, sqlstr); ListBox2.DataSource = dr1; ListBox2.DataTextField = "username"; ListBox2.DataValueField = "username"; ListBox2.DataBind(); con1.Close();
} }FORM.CS using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace RoleManager { public partial class Form1 : Form { public Form1() { InitializeComponent(); GetData roles = new GetData(); listBox2.DataSource = roles.getRoles();
参考代码: protected void Button3_Click(object sender, EventArgs e) { //int id = int.Parse(Request.QueryString["fid"].ToString()); int id = 15002; string constr1 = ConfigurationManager.AppSettings["cn"].ToString(); SqlConnection con = new SqlConnection(constr1); con.Open(); for (int i = 0; i < ListBox1.Items.Count; i++) {
string a = ListBox1.Items[i].ToString(); string sqlstr = "select * from yz where yid= ' " + id + "'and bumen ='" +a+"'"; SqlCommand cmd2 = new SqlCommand(sqlstr, con); SqlDataReader sdr = cmd2.ExecuteReader(); if (!sdr.Read()) { SqlConnection con1 = new SqlConnection(constr1); con1.Open(); string sqlstr1 ="select username from yh where id='"+id+"'" ; SqlCommand cmd3 = new SqlCommand(sqlstr1, con1); SqlDataReader sdr1 = cmd3.ExecuteReader(); sdr1.Read(); Session["username"] = sdr1["username"].ToString(); sdr1.Close(); con1.Close(); SqlConnection con2 = new SqlConnection(constr1); con2.Open(); string sqlstr2 = "select id from yh where username='" + a + "'"; SqlCommand cmd4 = new SqlCommand(sqlstr2, con2); SqlDataReader sdr2 = cmd4.ExecuteReader(); sdr2.Read(); Session["id"] = sdr2["id"].ToString(); sdr2.Close(); con2.Close(); object yname = Session["username"]; object bid = Session["id"]; string sql1 = "insert yz (bumen,bid,yonghu,yid)values('" + a + "','" + bid + "','" + yname + "','" + id + "')"; //再 new一个连接,datareader时连接一直是打开的并且被datareader独享 SqlConnection con3 = new SqlConnection(constr1); SqlCommand cmd5 = new SqlCommand(sql1, con3); con3.Open(); cmd5.ExecuteNonQuery(); con3.Close();
DELETE FROM listbox2;点击确定时,同时操作插入和删除语句
for循环插入,同时删除。你绑定的时候不是绑定的主键吗,删除根据主键删除被。
int id = 15002;
string constr = ConfigurationManager.AppSettings["cn"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
string sqlstr = "select bumen from yz where yid = '"+id+"'";
SqlCommand cmd = new SqlCommand(sqlstr, con);
SqlDataReader dr = cmd.ExecuteReader();
//mydata.Fill(ds, sqlstr);
ListBox1.DataSource = dr ;
ListBox1.DataTextField = "bumen";
ListBox1.DataValueField = "bumen";
ListBox1.DataBind();
con.Close();
SqlConnection con1 = new SqlConnection(constr);
con1.Open();
string sqlstr1 = "select username from yh where id <='10000' and id not in (select bid from yz where yid = '"+id+"')";
SqlCommand cmd1 = new SqlCommand(sqlstr1, con1);
SqlDataReader dr1 = cmd1.ExecuteReader();
//mydata.Fill(ds, sqlstr);
ListBox2.DataSource = dr1;
ListBox2.DataTextField = "username";
ListBox2.DataValueField = "username";
ListBox2.DataBind();
con1.Close();
如果连爬都没学会,还是不要想怎么跑了.遇到问题要先将问题分解:
1.已经知道要插入和删除的数据,如何实现插入和删除?
涉及到SQL语句语法,程序调用数据库接口
2.LISTBOX里的数据是从哪里来的,点击按钮后会相应的出现什么变化?
3.什么时候执行数据库插入和删除,每移动一次就执行一次么?先把需求变成方案,方案实现起来遇到问题就直接百度GOOGLE相应的问题
而不是直接把需求贴上来求代码.
到底是你做程序还是我做程序啊.
你程序里到底要执行个什么逻辑,具体实现个什么效果,你得去设计啊.
LISTBOX里初始数据是从哪里来的,应该什么时候去查询?
LISTBOX里已经存在数据,如何删除一行,如何添加一行?看起来是个很小的问题,其实里面涉及到的东西多着呢.
如果你能搞明白以上我所说的这些知识点,你想实现你的功能是非常简单的.
如果你不明白,即使求得了代码,你又知道该往哪里粘?
小伙子不虚心啊.你知道了说知道了就行了.
既然LISTBOX控件你会操作,剩下的不就是将里面内容拼个SQL语句执行就行了
USE [Csdn]
GO/****** Object: Table [dbo].[role] Script Date: 06/25/2014 06:23:37 ******/
SET ANSI_NULLS ON
GOSET QUOTED_IDENTIFIER ON
GOCREATE TABLE [dbo].[role](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](12) NULL
) ON [PRIMARY]GO
GetData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
namespace RoleManager
{
class GetData
{
/// <summary>
/// 获得数据库连接SqlConnection;
/// </summary>
/// <returns></returns>
//public SqlConnection getConn()
//{
// string cs="Data Source=(local);Initial catalog=Csdn;User ID=sa;password='pacs1234'";
// return new SqlConnection(cs);
//}
public ArrayList getRoles()
{
ArrayList Roles = new ArrayList();
//string cs = "Data Source=(local);Initial catalog=Csdn;User ID=sa;password='pacs1234'";
string cs = "uid=sa;pwd=pacs1234;initial catalog=Csdn;data source=192.168.1.102";
SqlConnection con = new SqlConnection(cs);
con.Open();
try
{
SqlCommand cmd = new SqlCommand("select name from Csdn.dbo.role", con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Roles.Add(reader["name"].ToString());
}
reader.Close();
return Roles;
}
catch (Exception)
{
return Roles;
}
}
}
}FORM.CS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RoleManager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GetData roles = new GetData();
listBox2.DataSource = roles.getRoles();
} private void button1_Click(object sender, EventArgs e)
{
string lb2 = listBox2.SelectedItem.ToString();
listBox1.Items.Add(lb2);
} private void button2_Click(object sender, EventArgs e)
{ } private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{ }
}
}
protected void Button3_Click(object sender, EventArgs e)
{
//int id = int.Parse(Request.QueryString["fid"].ToString());
int id = 15002;
string constr1 = ConfigurationManager.AppSettings["cn"].ToString();
SqlConnection con = new SqlConnection(constr1);
con.Open();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
string a = ListBox1.Items[i].ToString();
string sqlstr = "select * from yz where yid= ' " + id + "'and bumen ='" +a+"'";
SqlCommand cmd2 = new SqlCommand(sqlstr, con);
SqlDataReader sdr = cmd2.ExecuteReader();
if (!sdr.Read())
{
SqlConnection con1 = new SqlConnection(constr1);
con1.Open();
string sqlstr1 ="select username from yh where id='"+id+"'" ;
SqlCommand cmd3 = new SqlCommand(sqlstr1, con1);
SqlDataReader sdr1 = cmd3.ExecuteReader();
sdr1.Read();
Session["username"] = sdr1["username"].ToString();
sdr1.Close();
con1.Close(); SqlConnection con2 = new SqlConnection(constr1);
con2.Open();
string sqlstr2 = "select id from yh where username='" + a + "'";
SqlCommand cmd4 = new SqlCommand(sqlstr2, con2);
SqlDataReader sdr2 = cmd4.ExecuteReader();
sdr2.Read();
Session["id"] = sdr2["id"].ToString();
sdr2.Close();
con2.Close();
object yname = Session["username"];
object bid = Session["id"]; string sql1 = "insert yz (bumen,bid,yonghu,yid)values('" + a + "','" + bid + "','" + yname + "','" + id + "')";
//再 new一个连接,datareader时连接一直是打开的并且被datareader独享
SqlConnection con3 = new SqlConnection(constr1);
SqlCommand cmd5 = new SqlCommand(sql1, con3);
con3.Open();
cmd5.ExecuteNonQuery();
con3.Close();
}
sdr.Close();
}
con.Close();
}