Ejemplo n.º 1
0
        private void cbxProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            AreaInfo provinceAreaInfo = this.cbxProvince.SelectedItem as AreaInfo;

            //判断是否 拿到的城市为空
            if (provinceAreaInfo == null)
            {
                return;
            }
            //根据省的Id获取所有的城市信息

            //加载数据库中的所有的省的数据
            string connStr = ConfigurationManager.ConnectionStrings["sqlConn"].ConnectionString;

            //创建链接对象
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    conn.Open();//***********8
                    cmd.CommandText = @"select AreaId, AreaName, AreaPid from [dbo].[AreaFull] where AreaPId=" + provinceAreaInfo.AreaId;
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        this.cbxCity.Items.Clear();
                        while (reader.Read())
                        {
                            //int AreadId = int.Parse(reader["AreaId"].ToString());
                            //把表格的数据转换成 对象数据
                            AreaInfo areaInfo = new AreaInfo();
                            areaInfo.AreaId   = int.Parse(reader["AreaId"].ToString());
                            areaInfo.AreaName = reader["AreaName"].ToString();
                            areaInfo.AreaPId  = int.Parse(reader["AreaPId"].ToString());
                            //把省的信息放到 ComboBox中。ComboBox显示信息是 Item对象的ToString()
                            this.cbxCity.Items.Add(areaInfo);
                        }
                    } //end useing  reader
                }     //end  useing cmd
            }         //en
            this.cbxCity.SelectedIndex = 0;
        }