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; using System.Data.SqlClient; using System.Configuration;
namespace 省市聯動 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { Tsql.GetConnection(); using (SqlConnection conn = new SqlConnection(Tsql.GetConnection())) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from promary"; using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { Province pr = new Province(); pr.proID = dr.GetInt32(dr.GetOrdinal("proID")); //這里要特別注意數據庫字段的類型 proID在數據庫里是int型 pr.proName = dr.GetString(dr.GetOrdinal("proName")); comboBox1.Items.Add(pr);//pr是一個Province對象。Province有一個proName和proID屬性。我將一個對象填充到comboBox1里。再讓comboBox1的DisplayMember屬性設置proName?!疽簿褪亲宑omboBox1顯示pr對象的proName屬性】。這樣在填充市級城市的時候我就可以找到當前選擇項comboBox1.SelectedItem(或者當前選擇對象)所對應的proID } } } comboBox1.SelectedIndex = 0; //將comcomboBox1的默認選項設為0 } }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { comboBox2.Items.Clear(); //在選擇的時候先清空一下combox1里面的所以項,以免在更換省名的時候,與先前對應的省名下的市沒有刪除。 Province tem = (Province)comboBox1.SelectedItem; // 因為在comboBox1.Items.Add(pr)里添加的是對象。所以在這里講comBox1所選中的當前對象賦給tem對象 (tem對象下面有兩個屬性 proName和proID)
int id = tem.proID; //將當前選中對象的的proID賦值給id,給下面的sql查詢語句使用 Tsql.GetConnection();// 獲取數據庫連接字符串。 using (SqlConnection conn = new SqlConnection(Tsql.GetConnection())) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) {
cmd.CommandText = "select * from city where proID=@id"; cmd.Parameters.Add(new SqlParameter("id", id)); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { comboBox2.Items.Add(dr.GetString(dr.GetOrdinal("cityName"))); } } } comboBox2.SelectedIndex = 0; //將comcomboBox1的默認選項設為0 } } } class Tsql { public static string GetConnection() //數據庫連接字符串 { string getConn = ConfigurationManager.ConnectionStrings["getConn"].ConnectionString; return getConn; } } class Province { public string proName { get; set; } public int proID { get; set; } } }
using System.Data.SqlClient; namespace 省市選擇2 { public partial class Form1 : Form { publicForm1() { InitializeComponent(); } privatevoid Form1_Load(objectsender,EventArgs e) { /* //測試代碼:執行這段代碼的情況下是可以吧 tom這個值添加到 comboBox1中的。 People p1 = new People(); p1.name = "tom"; p1.age = 25; comboBox1.Items.Add(p1.name); */ using(SqlConnection conn =newSqlConnection("Data source=凡斌-VAIO;Initialcatalog=DBPromary;integrated security=true")) { conn.Open(); using(SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from promary"; using(SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { //string str =reader.GetString(reader.GetOrdinal("proName")); //comboBox1.Items.Add(str); Province pv =new Province(); //將comboBox1控件的DisplayMember屬性設置為“ProvinceName”用來在comboBox1控件中顯示ProvinceName屬性的值 comboBox1.DisplayMember = "ProvinceName"; pv.ProvinceName =reader.GetString(reader.GetOrdinal("proName")); pv.ProvinceID =reader.GetInt32(reader.GetOrdinal("proID")); //雖然pv屬于對象,把一個對象添加到ComboxBox1中,控件顯示的值為對象的類名,但是在前面comboBox1.DisplayMember ="ProvinceName"處已經設置好控件要顯示的為 Province類的ProvinceName值,所以添加進去的是pv對象的ProvinceName值 comboBox1.Items.Add(pv); } } } } }
privatevoid comboBox1_SelectedIndexChanged(object sender,EventArgse) //注意這里是comboBox1不是comboBox2 { try { Province tem = (Province) comboBox1.SelectedItem; int id = tem.ProvinceID;
comboBox2.Items.Clear(); using(SqlConnection conn =newSqlConnection("Data source=凡斌-VAIO;Initialcatalog=DBPromary;integrated security=true")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { //查詢city(市)表里的 proID與promary(省)表里proID相同的所有數據。 cmd.CommandText = "select * from city where proID =@id"; cmd.Parameters.Add(new SqlParameter("id", id)); SqlDataReader dr = cmd.ExecuteReader();
class Province { public string ProvinceName { get;set; } public int ProvinceID { get;set; } } //class People //{ // public string name { get; set; } // public int age { get; set; } //} }