时间:2021-07-01 10:21:17 帮助过:18人阅读
1 create table Dic_City 2 ( 3 id int NOT NULL, 4 cityID varchar(6) default NULL, 5 city varchar(50) default NULL, 6 provinceid varchar(6) default NULL, 7 PRIMARY KEY (id) 8 )Dic_City
1 CREATE TABLE Dic_Area 2 ( 3 id int NOT NULL, 4 areaID varchar(50) default NULL, 5 area varchar(60) default NULL, 6 cityID varchar(6) default NULL, 7 primary key(id) 8 )Dic_Area
插入数据,(数据来源https://www.cnblogs.com/zhu520/p/8244578.html),因为实在是有点懒,只能网上借用一下这位兄台的数据了。插入数据就不说了,复制或者下载过来改一下表名执行就ok。
二、新建一个Windows窗体应用,插入3个ComboBox,命名分别为:cbo_Province、cbo_City、cbo_District。
先声明一个连接字符串:
1 private string connStr = @"Data Source=SD-20191219LHFX\SQLEXPRESS;Initial Catalog=AreaSelection;User ID=sa;Password=***********"; //连接字符串
接下来在Form1_Load中添加预加载代码,程序运行的时候ComboBox中就要加载显示地区信息。
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 LoadingProvice(); 4 }
为了代码清晰,我把省市区分别封装成三个方法:LoadingProvice()、LoadingCity()、LoadingDistrict()。
但是在这之前,我们要先构造一个集合,因为cbo_Province可以调用DataSource这个方法。三个表中,每个表都有ID和地区名字,所以新建一个类AreaInfo.cs就可以了:
在AreaInfo中添加下面的代码:
1 public partial class AreaInfo 2 { 3 public int Id { get; set; } //ID 4 public string Title { get; set; } //地区名字 5 }
接下来就可以写方法了:(以下分别是省、市、区)
1 private void LoadingProvice() 2 { 3 List<AreaInfo> list = new List<AreaInfo>(); 4 using(SqlConnection conn = new SqlConnection(connStr)) 5 { 6 string sql = "select * from Dic_Province"; 7 SqlCommand cmd = new SqlCommand(sql, conn); 8 conn.Open(); 9 SqlDataReader reader = cmd.ExecuteReader(); 10 11 while (reader.Read()) 12 { 13 //添加元素 14 list.Add(new AreaInfo() 15 { 16 //初始化器 17 Id = Convert.ToInt32(reader["ProvinceID"]), 18 Title = reader["Province"].ToString() 19 }); 20 } 21 } 22 cbo_Province.DisplayMember = "Title"; //显示属性 23 cbo_Province.ValueMember = "Id"; //值属性 24 cbo_Province.DataSource = list; 25 }
1 private void LoadingCity() 2 { 3 int pid = Convert.ToInt32(cbo_Province.SelectedValue); 4 List<AreaInfo> list = new List<AreaInfo>(); 5 string sql = "select * from Dic_City where provinceid = @pid"; 6 using(SqlConnection conn = new SqlConnection(connStr)) 7 { 8 SqlCommand cmd = new SqlCommand(sql, conn); 9 cmd.Parameters.Add(new SqlParameter("@pid", pid)); 10 conn.Open(); 11 SqlDataReader reader = cmd.ExecuteReader(); 12 while(reader.Read()) 13 { 14 list.Add(new AreaInfo() 15 { 16 Id = Convert.ToInt32(reader["cityID"]), 17 Title = reader["city"].ToString() 18 }); 19 } 20 } 21 cbo_City.DisplayMember = "Title"; 22 cbo_City.ValueMember = "Id"; 23 cbo_City.DataSource = list; 24 }
1 private void LoadingDistrict() 2 { 3 List<AreaInfo> list = new List<AreaInfo>(); 4 int cid = Convert.ToInt32(cbo_City.SelectedValue); 5 string sql = "select * from Dic_Area where cityID = @cid"; 6 using(SqlConnection conn = new SqlConnection(connStr)) 7 { 8 SqlCommand cmd = new SqlCommand(sql, conn); 9 cmd.Parameters.Add(new SqlParameter("@cid", cid)); 10 conn.Open(); 11 SqlDataReader reader = cmd.ExecuteReader(); 12 while(reader.Read()) 13 { 14 list.Add(new AreaInfo() 15 { 16 Id = Convert.ToInt32(reader["areaID"]), 17 Title = reader["area"].ToString() 18 }); 19 } 20 } 21 cbo_District.DisplayMember = "Title"; 22 cbo_District.ValueMember = "Id"; 23 cbo_District.DataSource = list; 24 }
每个方法大同小异,都是对数据库的基本操作,就没进行过多的注释。
最后在comboBox的SelectedIndexChanged方法中添加方法名,如下:
1 private void cbo_provice_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 LoadingCity(); 4 LoadingDistrict(); 5 }
1 private void cbo_City_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 LoadingDistrict(); 4 }
cbo_District控件下面就不需要添加方法了,因为区下面就没有分类了。
完成界面如下:
SQLServer中实现简单的省市区联动
标签:mysql city select 初始化 ade http nec 连接字符串 添加元素