1.city:城市数据库表
字段如下:
CityID:城市的id;
cityName:城市的名称,主键;
proID:外键id。-----》对应主键表promary.
2.省和直辖市表
proID:省id是主键。
ProName:省名称和直辖市名称
如图2:
Select TOp 1000 cityID,CityName,proID from city where proID=13 //ptoID是省id
所以13是安徽省的主键id,所以查询出来的是安徽省下的所有城市。
1.运行的时候绑定省绑定到comboDox上。 窗体程序运行加载到comboBox上。
a.新建窗体程序,拖入两个comboBox控件,属性下【设计】(name): cboProvince(省),cboCity(城市))
private void Form1_Load(object sender, EventArgs e) { this.LoadProvinceData(); } //加载省 private void LoadProvinceData() { using (SqlConnection conn = new SqlConnection(ConnString)) { string sql = "select ProID,ProName from promary"; using (SqlCommand cmd = new SqlCommand(sql,conn)) { using(SqlDataReader dr=cmd.ExecuteReader()) { while (dr.Read()) { int ProId =Convert.ToInt32(dr["proID"]); string proName =Convert.ToString(dr["ProName"]); cboProvince.Items.Add(proName);//绑定到comboBox } } } } }
运行效果如下:
2.(版本2)运行的时候省绑定到comboDox上。
(在Promary(省类)中加:虚方法tostring,重在proname)
public class Promary { int proID; public int ProID { get { return proID; } set { proID = value; } } string proName; public string ProName { get { return proName; } set { proName = value; } } //虚方法tostring,重在proname;子类没有到,就载父类的。
public override string ToString() { return proName; } } }
public partial class Form1 : Form { String ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cboProvince.DataSource = LoadPromaryList();//绑定数据到comboxBox控件上 }
/// <summary> /// 得到所有的省 /// </summary> /// <returns></returns> private List<Promary> LoadPromaryList() { List<Promary> allPromary = new List<Promary>();//集合 string sql = "select proID,proName from promary "; SqlDataReader dr = CommonCode.Sqlhelper.ExecuteReader(sql); while (dr.Read()) { Promary onePromary = new Promary(); onePromary.ProID = Convert.ToInt32(dr["proID"]); onePromary.ProName = dr["proName"].ToString(); allPromary.Add(onePromary);//添加到集合中 } dr.Close(); return allPromary; }
运行效果如下:
3.完整省市联动
用户选择省,就会触发一个属性SelectedIndexChanged(属性值更改时发生)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace _20121130 { public partial class Form1 : Form { String ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cboProvince.DisplayMember = "ProName";//省 cboProvince.ValueMember = "ProId";//省id cboProvince.DataSource = LoadPromaryList(); } /// <summary> /// 得到所有的省 /// </summary> /// <returns></returns> private List<Promary> LoadPromaryList() { List<Promary> allPromary = new List<Promary>(); string sql = "select proID,proName from promary "; SqlDataReader dr = CommonCode.Sqlhelper.ExecuteReader(sql); while (dr.Read()) { Promary onePromary = new Promary(); onePromary.ProID = Convert.ToInt32(dr["proID"]); onePromary.ProName = dr["proName"].ToString(); allPromary.Add(onePromary); } dr.Close(); return allPromary; } //用户选择省,就会触发一个属性SelectedIndexChanged事件,
//这个事件怎么出来的?步骤:1.单击属性comboBox省(控件)->SelectedIndexChanged事件(双击)
private void cboProvince_SelectedIndexChanged(object sender, EventArgs e) { cboCity.Items.Clear(); int proId = Convert.ToInt32(cboProvince.SelectedValue); string sql = @"SELECT [cityID] ,[cityName] ,[proID] FROM [DBPromary].[dbo].[city] where proID=@proId"; SqlDataReader dr = CommonCode.Sqlhelper.ExecuteReader(sql, new SqlParameter("@proId", proId)); while (dr.Read()) { cboCity.Items.Add(dr["cityName"].ToString()); } dr.Close();//如果使用的是Sqlhelper中的dr,一定要close,为了断开conn if (cboCity.Items.Count > 0) { cboCity.SelectedIndex = 0; } } } }
public class Promary { int proID; public int ProID { get { return proID; } set { proID = value; } } string proName; public string ProName { get { return proName; } set { proName = value; } } //public override string ToString() //{ // return proName; //} } }
运行效果如下:达到联动的效果