实现二级联动菜单 C#,带完整数据库 SQL
2006-03-14 15:15
447 查看
----------------db.cs------------------------------
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection createConnection()
{
SqlConnection con=new SqlConnection("server=.;database=list;uid=sa;pwd=;");
return con;
}
}
}
--------------------------.cs源文件--------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection con=db.createConnection();
con.Open();
SqlCommand cmd=new SqlCommand("select * from sheng",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.sheng.DataSource=sdr;
this.sheng.DataTextField="sheng";
this.sheng.DataValueField="shengid";
this.sheng.DataBind();
sdr.Close();
cmd.CommandText="select * from city where shengid=1";
sdr=cmd.ExecuteReader();
this.city.DataSource=sdr;
this.city.DataTextField="cityname";
this.city.DataValueField="cityid";
this.city.DataBind();
sdr.Close();
con.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sheng.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);
this.rdl.SelectedIndexChanged += new System.EventHandler(this.drl_SelectedIndexChanged);
this.mar.SelectedIndexChanged += new System.EventHandler(this.mar_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
SqlConnection con=db.createConnection();
con.Open();
SqlCommand cmd=new SqlCommand("select * from city where shengid="+this.sheng.SelectedValue.ToString(),con);
SqlDataReader sdr=cmd.ExecuteReader();
this.city.DataSource=sdr;
this.city.DataTextField="cityname";
this.city.DataValueField="cityid";
this.city.DataBind();
sdr.Close();
con.Close();
}
-----------------------sql文件-----------------------
create database list
use list
create table sheng
(
shengid int primary key,
sheng varchar(50) not null,
)
insert into sheng values(1,'四川')
insert into sheng values(2,'安徽')
insert into sheng values(3,'河南')
insert into sheng values(4,'重庆')
insert into sheng values(5,'广西')
select * from sheng
create table city
(
cityid int primary key,
shengid int foreign key references sheng(shengid),
cityname varchar(50) not null,
)
insert into city values(1,1,'成都')
insert into city values(2,1,'绵阳')
insert into city values(3,1,'广元')
insert into city values(4,2,'合肥')
insert into city values(5,2,'蚌埠')
insert into city values(6,2,'淮南')
insert into city values(7,3,'郑州')
insert into city values(8,3,'开封')
insert into city values(10,3,'商丘')
insert into city values(11,4,'垫江')
insert into city values(12,4,'江津')
insert into city values(13,4,'重庆')
insert into city values(14,5,'南宁')
insert into city values(15,5,'柳州')
insert into city values(16,5,'白色')
select * from city
---------如果有朋友学习要用的话最好看清楚,呵呵,我是边做边写的,并不是可以一次执行成功的------
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection createConnection()
{
SqlConnection con=new SqlConnection("server=.;database=list;uid=sa;pwd=;");
return con;
}
}
}
--------------------------.cs源文件--------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection con=db.createConnection();
con.Open();
SqlCommand cmd=new SqlCommand("select * from sheng",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.sheng.DataSource=sdr;
this.sheng.DataTextField="sheng";
this.sheng.DataValueField="shengid";
this.sheng.DataBind();
sdr.Close();
cmd.CommandText="select * from city where shengid=1";
sdr=cmd.ExecuteReader();
this.city.DataSource=sdr;
this.city.DataTextField="cityname";
this.city.DataValueField="cityid";
this.city.DataBind();
sdr.Close();
con.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sheng.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);
this.rdl.SelectedIndexChanged += new System.EventHandler(this.drl_SelectedIndexChanged);
this.mar.SelectedIndexChanged += new System.EventHandler(this.mar_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
SqlConnection con=db.createConnection();
con.Open();
SqlCommand cmd=new SqlCommand("select * from city where shengid="+this.sheng.SelectedValue.ToString(),con);
SqlDataReader sdr=cmd.ExecuteReader();
this.city.DataSource=sdr;
this.city.DataTextField="cityname";
this.city.DataValueField="cityid";
this.city.DataBind();
sdr.Close();
con.Close();
}
-----------------------sql文件-----------------------
create database list
use list
create table sheng
(
shengid int primary key,
sheng varchar(50) not null,
)
insert into sheng values(1,'四川')
insert into sheng values(2,'安徽')
insert into sheng values(3,'河南')
insert into sheng values(4,'重庆')
insert into sheng values(5,'广西')
select * from sheng
create table city
(
cityid int primary key,
shengid int foreign key references sheng(shengid),
cityname varchar(50) not null,
)
insert into city values(1,1,'成都')
insert into city values(2,1,'绵阳')
insert into city values(3,1,'广元')
insert into city values(4,2,'合肥')
insert into city values(5,2,'蚌埠')
insert into city values(6,2,'淮南')
insert into city values(7,3,'郑州')
insert into city values(8,3,'开封')
insert into city values(10,3,'商丘')
insert into city values(11,4,'垫江')
insert into city values(12,4,'江津')
insert into city values(13,4,'重庆')
insert into city values(14,5,'南宁')
insert into city values(15,5,'柳州')
insert into city values(16,5,'白色')
select * from city
---------如果有朋友学习要用的话最好看清楚,呵呵,我是边做边写的,并不是可以一次执行成功的------
相关文章推荐
- WheelView实现省市区三级联动(数据库实现版本号附带完整SQL及数据)
- js实现的全国省市二级联动下拉选择菜单完整实例
- WheelView实现省市区三级联动(数据库实现版本附带完整SQL及数据)
- 用ajax技术读取数据库内容实现二级联动下拉选择菜单
- jsp从数据库获取数据填充下拉框实现二级联动菜单的方法
- js实现的全国省市二级联动下拉选择菜单完整实例
- jsp从数据库获取数据填充下拉框实现二级联动菜单的方法
- WheelView实现省市区三级联动(数据库实现版本号附带完整SQL及数据)
- (转贴)实现带有数据绑定的客户端脚本控制的二级联动菜单
- JavaScript之AJAX实现二级联动菜单(jsp+js+servlet+xml)
- 二级联动菜单动态刷新下拉列表的一种实现方法
- 第十八篇——JDBC操作数据库之二级菜单联动
- java实现的二级联动菜单效果
- 基于asp+ajax和数据库驱动的二级联动菜单
- JQuery EasyUI Combobox 实现省市二级联动菜单
- jquery实现二级联动,实时查询数据库
- JavaScript实际应用:简单二级联动菜单实现
- JQuery实现的二级联动菜单