SQLServer 實現簡單的省市區聯動

今天研究了一下SQL Server實現省市區聯動的方法,記錄一下。html

1、先建立三個表,Dic_Area(區)、Dic_City(市)和Dic_Province(省),三個表建表語句以下:mysql

1 create table Dic_Province
2 (
3   id int NOT NULL,
4   provinceID varchar(6) default NULL,
5   province varchar(40) default NULL,
6   PRIMARY KEY  (id)
7 )
Dic_Province
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

插入數據,(數據來源http://www.javashuo.com/article/p-vtelnzqv-cd.html),由於實在是有點懶,只能網上借用一下這位兄臺的數據了。插入數據就不說了,複製或者下載過來改一下表名執行就ok。sql

 

2、新建一個Windows窗體應用,插入3個ComboBox,命名分別爲:cbo_Province、cbo_City、cbo_District。數據庫

先聲明一個鏈接字符串:ide

1 private string connStr = @"Data Source=SD-20191219LHFX\SQLEXPRESS;Initial Catalog=AreaSelection;User ID=sa;Password=***********"; //鏈接字符串

接下來在Form1_Load中添加預加載代碼,程序運行的時候ComboBox中就要加載顯示地區信息。spa

1 private void Form1_Load(object sender, EventArgs e)
2 {
3      LoadingProvice();
4 }

爲了代碼清晰,我把省市區分別封裝成三個方法:LoadingProvice()、LoadingCity()、LoadingDistrict()。3d

可是在這以前,咱們要先構造一個集合,由於cbo_Province能夠調用DataSource這個方法。三個表中,每一個表都有ID和地區名字,因此新建一個類AreaInfo.cs就能夠了:code

在AreaInfo中添加下面的代碼:orm

1 public partial class AreaInfo
2 {
3     public int Id { get; set; } //ID
4     public string Title { get; set; } //地區名字
5 }

接下來就能夠寫方法了:(如下分別是省、市、區)htm

 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控件下面就不須要添加方法了,由於區下面就沒有分類了。

完成界面以下:

相關文章
相關標籤/搜索