代码如下- private void Form1_Load(object sender, EventArgs e)
- {
- using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\ss.mdf;Integrated Security=True;User Instance=True"))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = "select * from promary";
- using (SqlDataReader dataReader = cmd.ExecuteReader())
- {
- while (dataReader.Read())
- {
- province item = new province();
- item.id = dataReader.GetInt32(dataReader.GetOrdinal("proID"));
- item.name = dataReader.GetString(dataReader.GetOrdinal("proName"));
- cmbsheng.Items.Add(item);
- }
- }
- }
- }
- }
复制代码- class province
- {
- public string name { get; set; }
- public int id { get; set; }
- }
复制代码- private void cmbsheng_SelectedIndexChanged(object sender, EventArgs e)
- {
- province item = (province)cmbsheng.SelectedItem;
- int proID = item.id;
- using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\ss.mdf;Integrated Security=True;User Instance=True"))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = "select * from city where proID=@proID";
- cmd.Parameters.Add(new SqlParameter("proID", proID));
- using (SqlDataReader dataReader = cmd.ExecuteReader())
- {
- while (dataReader.Read())
- {
- string cityName = dataReader.GetString(dataReader.GetOrdinal("cityName"));
- cmbshi.Items.Add(cityName);
- }
- }
- }
- }
- }
复制代码 数据库问题,就是运行后出现个问题,不明白怎么解决,
选择一个省后再选择另外一个省,市的下拉菜单没有重置,求解决... |