首页 > 代码库 > 几个数据库的小案例(二):极其简单的省市联动

几个数据库的小案例(二):极其简单的省市联动

总用有两个文件(frmMain.cs SqlHelper.cs)

//frmMain.cs
//作者:Me
using
System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 省市联动小案例 { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } //private void ClearAll() //{ // cbCity.Items.Clear(); // cbProvince.Items.Clear(); // cbTown.Items.Clear(); //} private void Form1_Load(object sender, EventArgs e) { //调用Load,加载省 SqlHelper.Load(cbProvince, 0); } private void cbProvince_SelectedIndexChanged(object sender, EventArgs e) { cbCity.Items.Clear(); //调用GetAreaId,获取省的id int proId = SqlHelper.GetAreaId(cbProvince.SelectedItem.ToString()); //调用Load,加载市 SqlHelper.Load(cbCity, proId); } private void cbCity_SelectedIndexChanged(object sender, EventArgs e) { cbTown.Items.Clear(); //调动GetAreaId,获得市的id int cityId = SqlHelper.GetAreaId(cbCity.SelectedItem.ToString()); //调用Load方法,加载镇 SqlHelper.Load(cbTown, cityId); } private void btnShow_Click(object sender, EventArgs e) { //MessageBox.Show(cbProvince.SelectedItem.ToString()+":"+cbCity.SelectedItem.ToString()+":"+cbTown.SelectedItem.ToString()); string province = ""; string city = ""; string town = ""; if (cbProvince.SelectedIndex != -1) { province = cbProvince.SelectedItem.ToString(); } if (cbCity.SelectedIndex != -1) { city = ":" + cbCity.SelectedItem.ToString(); } if (cbTown.SelectedIndex != -1) { town = ":" + cbTown.SelectedItem.ToString(); } MessageBox.Show(province+city+town); } } }

以下是SqlHelper.cs

//SqlHelper.cs
//作者:Me
using
System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Windows.Forms; namespace 省市联动小案例 { class SqlHelper { //连接字符串 private static string sqlStr = "server=.;database=WebSite;uid=sa;pwd=123456"; /// <summary> /// 通过名称获取id /// </summary> /// <param name="province">名字</param> /// <returns>id</returns> public static int GetAreaId(string province) { using (SqlConnection conn = new SqlConnection(sqlStr)) { using (SqlCommand cmd = conn.CreateCommand()) { SqlDataReader dr = null; cmd.CommandText = "select ar_id from Area where ar_name = @name"; cmd.Parameters.Add(new SqlParameter("@name", province)); try { conn.Open(); dr = cmd.ExecuteReader(); } catch (Exception ex) { MessageBox.Show("数据库错误:\n" + ex.Message); return 0; } dr.Read(); return dr.GetInt32(0); } } } /// <summary> /// 通过id,加载名字 /// </summary> /// <param name="cbCity">cbo控件</param> /// <param name="proId">id</param> public static void Load(ComboBox cbCity, int proId) { using (SqlConnection conn = new SqlConnection(sqlStr)) { using (SqlCommand cmd = conn.CreateCommand()) { SqlDataReader dr = null; cmd.CommandText = "select ar_name from Area where ar_uid = @proId"; cmd.Parameters.Add(new SqlParameter("@proId", proId)); try { conn.Open(); dr = cmd.ExecuteReader(); } catch (Exception ex) { MessageBox.Show("数据库错误:\n" + ex.Message); return; } if (dr.HasRows) { while (dr.Read()) { cbCity.Items.Add(dr[0].ToString()); } } } } } //End } }

各种控件的命名和属性

控件名  属性 事件
cbProvince   cbProvince_SelectedIndexChanged
cbCity   cbCity_SelectedIndexChanged
cbTown    
btnShow   btnShow_Click