省市区号邮编级联
之前曾经做过好几个这样的省市级联查询,几乎每个老师讲到ADO.NET的时候都会把这个例子拿出来,每次我都没法百分百的自己写出来,都要多多少少看看之前写的代码。。。所以今天就把这个记录下来了。。(其中的数据库就是用我之前博文里的数据库)
1 using System.Data.SqlClient; 2 3 namespace 省市区号级联 4 { 5 public partial class Form1 : Form 6 { 7 public Form1() 8 { 9 InitializeComponent(); 10 } 11 //创建一个Province类 12 public class Province 13 { 14 public string pName { get; set; } 15 public int pId { get; set; } 16 public override string ToString() 17 { 18 return this.pName; 19 } 20 } 21 //创建一个City类 22 public class City 23 { 24 public string cName { get; set; } 25 public int cId { get; set; } 26 public override string ToString() 27 { 28 return this.cName; 29 } 30 } 31 //窗体加载时给“省”下拉框加载数据 32 private void Form1_Load(object sender, EventArgs e) 33 { 34 string sql = "select pId, pName from province"; 35 SqlDataReader reader = SqlHelper.ExecuteReader(sql, null); 36 if (reader.HasRows) 37 { 38 while (reader.Read()) 39 { 40 cmbPro.Items.Add( 41 new Province 42 { 43 pName = reader.GetString(reader.GetOrdinal("pName")), 44 pId = reader.GetInt32(reader.GetOrdinal("pId")) 45 } 46 ); 47 } 48 } 49 //默认选择第一个省份 50 cmbPro.SelectedIndex = 0; 51 } 52 //当“省”下拉框选择了数据时,触发事件,引起“市”下拉框跟着改变 53 private void cmbPro_SelectedIndexChanged(object sender, EventArgs e) 54 { 55 //很重要!!改变省份时要将城市下拉框清空 56 cmbCity.Items.Clear(); 57 int pid = ((Province)cmbPro.SelectedItem).pId; 58 string sql = "select cid,cName from city where pid = @pid"; 59 SqlParameter[] para = { 60 new SqlParameter("@pid",pid) 61 }; 62 SqlDataReader reader = SqlHelper.ExecuteReader(sql, para); 63 if (reader.HasRows) 64 { 65 while (reader.Read()) 66 { 67 cmbCity.Items.Add( 68 new City 69 { 70 cName = reader.GetString(reader.GetOrdinal("cName")), 71 cId = reader.GetInt32(reader.GetOrdinal("cid")) 72 } 73 ); 74 } 75 } 76 //默认选择第一个城市 77 cmbCity.SelectedIndex = 0; 78 } 79 //当“市”下拉框选择了数据时,触发事件,引起“区号”和“邮编”下拉框跟着改变 80 private void cmbCity_SelectedIndexChanged(object sender, EventArgs e) 81 { 82 int cid = ((City)cmbCity.SelectedItem).cId; 83 string sql = "select cpostcode,careanum from city where cid = @cid"; 84 SqlParameter[] para = { 85 new SqlParameter("@cid",cid) 86 }; 87 SqlDataReader reader = SqlHelper.ExecuteReader(sql, para); 88 if (reader.HasRows) 89 { 90 while (reader.Read()) 91 { 92 txtAreaNum.Text = reader.GetString(reader.GetOrdinal("cPostCode")); 93 txtPostCode.Text = reader.GetString(reader.GetOrdinal("cAreaNum")); 94 } 95 } 96 } 97 } 98 }
另外,在项目中添加SqlHelper类。。封装了ExecuteReader方法,这样调用会少写很多的代码!
1 using System.Data.SqlClient; 2 3 namespace 省市区号级联 4 { 5 public static class SqlHelper 6 { 7 //连接字符串,可根据自己的电脑和数据库更改 8 static string constr = "server=.;database=MyDB;uid=sa;pwd=xqt"; 9 //封装一个ExecuteReader方法 10 public static SqlDataReader ExecuteReader(string cmdText, params SqlParameter[] paras) 11 { 12 SqlConnection conn = new SqlConnection(constr); 13 using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 14 { 15 if (paras != null) 16 { 17 cmd.Parameters.AddRange(paras); 18 } 19 conn.Open(); 20 SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); 21 //SqlDataReader reader = cmd.ExecuteReader(); 22 cmd.Parameters.Clear(); 23 return reader; 24 } 25 } 26 } 27 }
可能这个用处不大。。但是保存下来总会有用到的一天的。。