C# 省市县三级级联 分类: .NET 2012-11-04 15:31 1541人阅读 评论(0) 收藏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace comback
{
    /// <summary>
    /// 1:要添加的省可以在下面的第18行中添加,相关市、区县等添加到下面声明变量处即可。
    /// 2:然后在计数处声明相关变量以供计算。
    /// 3:然后在execute()方法的“扩展处”进行计算。
    /// 4:然后在addCity()方法中添加省下辖市
    /// 5:没然后了,直接看181行哥的留言。。。
    /// </summary>
    class District
    {

        private int js = 1, zj=2, sh=3;

        //省
        private string[] province = new string[] { "--请选择--", "江苏省", "浙江省", "上海市" };

        //市
        private string[] city1 = new string[] { "--请选择--", "南京市", "无锡市", "徐州市", "常州市", "苏州市", "南通市", "连云港市", "淮安市", "盐城市", "扬州市", "镇江市", "泰州市", "宿迁市" };
        private string[] city2 = new string[] { "--请选择--", "杭州市", "宁波市", "温州市", "绍兴市", "湖州市", "金华市", "衢州市", "舟山市", "台州市", "丽水市", "嘉兴市" };

        //江苏省区县
        private string[] c1_1 = new string[] { "--请选择--", "玄武区", "白下区", "秦淮区", "建邺区", "鼓楼区", "下关区", "浦口区", "六合区", "栖霞区", "雨花台区", "江宁区", "溧水县", "高淳县" };
        private string[] c1_2 = new string[] { "--请选择--", "崇安区", "南长区", "北塘区", "惠山区", "锡山区", "滨湖区", "江阴市", "宜兴市" };
        private string[] c1_3 = new string[] { "--请选择--", "云龙区", "鼓楼区", "贾汪区", "泉山区", "铜山区", "邳州市", "新沂市", "睢宁县", "沛 县", "丰 县" };
        private string[] c1_4 = new string[] { "--请选择--", "天宁区", "钟楼区", "新北区", "武进区", "金坛市", "溧阳市" };
        private string[] c1_5 = new string[] { "--请选择--", "吴中区", "相城区", "金阊区", "平江区", "沧浪区", "虎丘区", "常熟市", "昆山市", "张家港市", "吴江市", "太仓市" };
        private string[] c1_6 = new string[] { "--请选择--", "崇川区", "港闸区", "通州区", "海安县", "如东县", "如皋市", "海门市", "启东市" };
        private string[] c1_7 = new string[] { "--请选择--", "新浦区", "连云区", "海州区", "赣榆县", "灌云县", "东海县", "灌南县" };
        private string[] c1_8 = new string[] { "--请选择--", "清河区", "清浦区", "楚州区", "淮阴区", "金湖县", "盱眙县", "洪泽县", "涟水县" };
        private string[] c1_9 = new string[] { "--请选择--", "亭湖区", "盐都区", "大丰市", "东台市", "建湖县", "射阳县", "阜宁县", "滨海县", "响水县" };
        private string[] c1_10 = new string[] { "--请选择--", "广陵区", "维扬区", "邗江区", "宝应县", "江都市", "高邮市", "仪征市" };
        private string[] c1_11 = new string[] { "--请选择--", "京口区", "润州区", "丹徒区", "镇江新区", "句容市", "丹阳市", "扬中市" };
        private string[] c1_12 = new string[] { "--请选择--", "海陵区", "高港区", "兴化市", "靖江市", "泰兴市", "姜堰市" };
        private string[] c1_13 = new string[] { "--请选择--", "沭阳县", "泗阳县", "泗洪县", "宿豫区", "宿城区" };

        //浙江省区县
        private string[] c2_1 = new string[] { "--请选择--", "上城区", "下城区", "江干区", "拱墅区", "西湖区", "滨江区", "萧山区", "余杭区", "建德市", "富阳市", "临安市", "桐庐县", "淳安县" };
        private string[] c2_2 = new string[] { "--请选择--", "海曙区", "江东区", "江北区", "北仑区", "镇海区", "鄞州区", "余姚市", "慈溪市", "奉化市", "象山县", "宁海县" };
        private string[] c2_3 = new string[] { "--请选择--", "鹿城区", "瓯海区", "龙湾区", "瑞安市", "乐清市", "永嘉县", "苍南县", "平阳县", "泰顺县", "文成县", "洞头县" };
        private string[] c2_4 = new string[] { "--请选择--", "绍兴县", "诸暨市", "上虞市", "嵊州市", "新昌县", "越城区" };
        private string[] c2_5 = new string[] { "--请选择--", "吴兴区", "南浔区", "长兴县", "德清县", "安吉县" };
        private string[] c2_6 = new string[] { "--请选择--", "婺城区", "金东区", "兰溪市", "永康市", "义乌市", "东阳市", "武义县", "浦江县", "磐安县" };
        private string[] c2_7 = new string[] { "--请选择--", "柯城区", "衢江区", "江山市", "常山县", "龙游县", "开化县" };
        private string[] c2_8 = new string[] { "--请选择--", "定海区", "普陀区", "岱山县", "嵊泗县" };
        private string[] c2_9 = new string[] { "--请选择--", "椒江区", "黄岩区", "路桥区", "临海市", "温岭市", "三门县", "天台县", "仙居县", "玉环县" };
        private string[] c2_10 = new string[] { "--请选择--", "莲都区", "龙泉市", "缙云县", "青田县", "云和县", "遂昌县", "松阳县", "庆元县", "景宁畲族自治县" };
        private string[] c2_11 = new string[] { "--请选择--", "南湖区", "秀洲区", "嘉善县", "海盐县", "平湖市", "海宁市", "桐乡市" };

        //上海市区县
        private string[] c3 = new string[] { "--请选择--", "浦东新区", "黄浦区", "卢湾区", "徐汇区", "长宁区", "静安区", "普陀区", "闸北区", "闸北区", "虹口区", "杨浦区", "宝山区", "闵行区", "嘉定区", "松江区", "青浦区", "奉贤区", "金山区", "崇明县" };

        //计数
        private int Len1_1, Len1_2, Len1_3, Len1_4, Len1_5, Len1_6, Len1_7, Len1_8, Len1_9, Len1_10, Len1_11, Len1_12, Len1_13; //江苏
        private int Len2_1, Len2_2, Len2_3, Len2_4, Len2_5, Len2_6, Len2_7, Len2_8, Len2_9, Len2_10, Len2_11;                   //浙江
        private int Len3;                                                                                                       //上海
        private int js3, js4, js5, js6, js7, js8, js9, js10, js11, js12, js13;                                                  //江苏
        private int zs3, zs4, zs5, zs6, zs7, zs8, zs9, zs10, zs11, zs12, zs13;                                                  //浙江
        private int ss3;                                                                                                        //上海
        
        /// <summary>
        /// 计数
        /// </summary>
        private void excute()
        {
            //获得江苏省下辖市下面的区县数组长度
            Len1_1 = this.c1_1.Length;
            Len1_2 = this.c1_2.Length;
            Len1_3 = this.c1_3.Length;
            Len1_4 = this.c1_4.Length;
            Len1_5 = this.c1_5.Length;
            Len1_6 = this.c1_6.Length;
            Len1_7 = this.c1_7.Length;
            Len1_8 = this.c1_8.Length;
            Len1_9 = this.c1_9.Length;
            Len1_10 = this.c1_10.Length;
            Len1_11 = this.c1_11.Length;
            Len1_12 = this.c1_12.Length;
            Len1_13 = this.c1_13.Length;

            //获得浙江省下辖市下面的区县数组长度
            Len2_1 = this.c2_1.Length;
            Len2_2 = this.c2_2.Length;
            Len2_3 = this.c2_3.Length;
            Len2_4 = this.c2_4.Length;
            Len2_5 = this.c2_5.Length;
            Len2_6 = this.c2_6.Length;
            Len2_7 = this.c2_7.Length;
            Len2_8 = this.c2_8.Length;
            Len2_9 = this.c2_9.Length;
            Len2_10 = this.c2_10.Length;
            Len2_11 = this.c2_11.Length;

            //获得上海市下辖市下面的区县数组长度
            Len3 = this.c3.Length;
            
            //此处获得扩展的省下辖市下面的区县数组长度


            //江苏【计算区县数组长度】
            js3 = Len1_1 + Len1_2;
            js4 = js3 + Len1_3;
            js5 = js4 + Len1_4;
            js6 = js5 + Len1_5;
            js7 = js6 + Len1_6;
            js8 = js7 + Len1_7;
            js9 = js8 + Len1_8;
            js10 = js9 + Len1_9;
            js11 = js10 + Len1_10;
            js12 = js11 + Len1_11;
            js13 = js12 + Len1_12;

            //浙江
            zs3 = js13 + Len1_13;
            zs4 = zs3 + Len2_1;
            zs5 = zs4 + Len2_2;
            zs6 = zs5 + Len2_3;
            zs7 = zs6 + Len2_4;
            zs8 = zs7 + Len2_5;
            zs9 = zs8 + Len2_6;
            zs10 = zs9 + Len2_7;
            zs11 = zs10 + Len2_8;
            zs12 = zs11 + Len2_9;
            zs13 = zs12 + Len2_10;

            //上海
            ss3 = zs13 + Len2_11;

            //此处计算扩展的省数组长度,计算方法为:在上一个计算的和基础上,加上上一个数组的最后下辖市的长度,并且计算到此省下的区县数组长度-1为止。

        }

        public DataSet getDS()
        {
            //计算一些数值
            excute();

            DataSet locations = new DataSet("location");

            DataTable table = new DataTable("province");
            table.Columns.Add("id", typeof(int));
            table.Columns.Add("name", typeof(string));
            table.PrimaryKey = new DataColumn[] { table.Columns[0] };
            locations.Tables.Add(table);
            //添加省
            addProvince(table);

            DataTable table2 = new DataTable("state");
            table2.Columns.Add("id", typeof(int));
            table2.Columns.Add("pid", typeof(int));
            table2.Columns.Add("name", typeof(string));
            table2.PrimaryKey = new DataColumn[] { table2.Columns[0] };
            locations.Tables.Add(table2);
            //添加市
            addCity(table2);

            DataTable table3 = new DataTable("county");
            table3.Columns.Add("id", typeof(int));
            table3.Columns.Add("pid", typeof(int));
            table3.Columns.Add("name", typeof(string));
            table3.PrimaryKey = new DataColumn[] { table3.Columns[0] };
            locations.Tables.Add(table3);

            //添加江苏下辖市下面的区县
            addJS(table3);

            //注意下面添加的代码,没有添加14这个数字。所以如果以后扩展其他省份的话,在每个省份之间,都要在原有省份的基础上+1。

            //添加浙江省下辖市的区县
            addZJ(table3);
            
            //添加上海市区县
            addSH(table3);
            
            //此处添加扩展的省市下的区县。。。需要手动创建方法

            //设置约束
            locations.Tables["state"].Constraints.Add(new ForeignKeyConstraint(locations.Tables["province"].Columns[0], locations.Tables["state"].Columns[1]));
            locations.Tables["county"].Constraints.Add(new ForeignKeyConstraint(locations.Tables["state"].Columns[0], locations.Tables["county"].Columns[1]));

            //设置关系
            DataRelation relation = new DataRelation("p2s", locations.Tables["province"].Columns[0], locations.Tables["state"].Columns[1]);
            locations.Relations.Add(relation);
            relation = new DataRelation("s2c", locations.Tables["state"].Columns[0], locations.Tables["county"].Columns[1]);
            locations.Relations.Add(relation);

            locations.AcceptChanges();

            return locations;
        }

        /// <summary>
        ///  添加省
        /// </summary>
        private void addProvince(DataTable table)
        {
            for (int i = 0; i < province.Length; i++)
            {
                table.Rows.Add(i, province[i]);
            }
        }

        /// <summary>
        /// 添加下辖市
        /// </summary>
        private void addCity(DataTable table2) 
        {
            //添加江苏省下辖市
            for (int i = 0; i < city1.Length; i++)
            {
                table2.Rows.Add(i, js, city1[i]);
            }
            //添加浙江省下辖市
            for (int i = 0; i < city2.Length; i++)
            {
                table2.Rows.Add(city1.Length + i, zj, city2[i]);
            }
            //添加上海市
            table2.Rows.Add(city1.Length + city2.Length + 1, sh, "上海市");

            //此处添加扩展的省下辖市。。

        }

        /// <summary>
        /// 添加江苏下辖市下面的区县
        /// </summary>
        /// <param name="table3"></param>
        private void addJS(DataTable table3)
        {
            for (int i = 0; i < Len1_1; i++)
            {
                table3.Rows.Add(i, 1, c1_1[i]);
            }
            for (int i = 0; i < Len1_2; i++)
            {
                table3.Rows.Add(Len1_1 + i, 2, c1_2[i]);
            }
            for (int i = 0; i < Len1_3; i++)
            {
                table3.Rows.Add(js3 + i, 3, c1_3[i]);
            }
            for (int i = 0; i < Len1_4; i++)
            {
                table3.Rows.Add(js4 + i, 4, c1_4[i]);
            }
            for (int i = 0; i < Len1_5; i++)
            {
                table3.Rows.Add(js5 + i, 5, c1_5[i]);
            }
            for (int i = 0; i < Len1_6; i++)
            {
                table3.Rows.Add(js6 + i, 6, c1_6[i]);
            }
            for (int i = 0; i < Len1_7; i++)
            {
                table3.Rows.Add(js7 + i, 7, c1_7[i]);
            }
            for (int i = 0; i < Len1_8; i++)
            {
                table3.Rows.Add(js8 + i, 8, c1_8[i]);
            }
            for (int i = 0; i < Len1_9; i++)
            {
                table3.Rows.Add(js9 + i, 9, c1_9[i]);
            }
            for (int i = 0; i < Len1_10; i++)
            {
                table3.Rows.Add(js10 + i, 10, c1_10[i]);
            }
            for (int i = 0; i < Len1_11; i++)
            {
                table3.Rows.Add(js11 + i, 11, c1_11[i]);
            }
            for (int i = 0; i < Len1_12; i++)
            {
                table3.Rows.Add(js12 + i, 12, c1_12[i]);
            }
            for (int i = 0; i < Len1_13; i++)
            {
                table3.Rows.Add(js13 + i, 13, c1_13[i]);
            }
        }

        /// <summary>
        /// 添加浙江省下辖市的区县
        /// </summary>
        /// <param name="table3"></param>
        private void addZJ(DataTable table3)
        {
            for (int i = 0; i < Len2_1; i++)
            {
                table3.Rows.Add(zs3 + i, 15, c2_1[i]);
            }
            for (int i = 0; i < Len2_2; i++)
            {
                table3.Rows.Add(zs4 + i, 16, c2_2[i]);
            }
            for (int i = 0; i < Len2_3; i++)
            {
                table3.Rows.Add(zs5 + i, 17, c2_3[i]);
            }
            for (int i = 0; i < Len2_4; i++)
            {
                table3.Rows.Add(zs6 + i, 18, c2_4[i]);
            }
            for (int i = 0; i < Len2_5; i++)
            {
                table3.Rows.Add(zs7 + i, 19, c2_5[i]);
            }
            for (int i = 0; i < Len2_6; i++)
            {
                table3.Rows.Add(zs8 + i, 20, c2_6[i]);
            }
            for (int i = 0; i < Len2_7; i++)
            {
                table3.Rows.Add(zs9 + i, 21, c2_7[i]);
            }
            for (int i = 0; i < Len2_8; i++)
            {
                table3.Rows.Add(zs10 + i, 22, c2_8[i]);
            }
            for (int i = 0; i < Len2_9; i++)
            {
                table3.Rows.Add(zs11 + i, 23, c2_9[i]);
            }
            for (int i = 0; i < Len2_10; i++)
            {
                table3.Rows.Add(zs12 + i, 24, c2_10[i]);
            }
            for (int i = 0; i < Len2_11; i++)
            {
                table3.Rows.Add(zs13 + i, 25, c2_11[i]);
            }
        }

        /// <summary>
        /// 添加上海下辖的区县
        /// </summary>
        /// <param name="table3"></param>
        private void addSH(DataTable table3)
        {
            for (int i = 0; i < Len3; i++)
            {
                //table3.Rows.Add(zs13 + Len3 + i, city1.Length + city2.Length + 1, c3[i]);
                table3.Rows.Add(ss3 + i, 27, c3[i]);
            }
        }

        //此处添加扩展的区县方法pid=29
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2012-11-04 15:31  哲夫  阅读(119)  评论(0编辑  收藏  举报