C# 获取行政区划代码(省、市、区、街道、村)
sql数据下载:http://download.csdn.net/download/linyongqin/10224874
获取省、市、区
网址:http://www.mca.gov.cn/article/sj/tjbz/a/2017/
获取省、市、区、街道、村
网址:http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/
统计用区划代码和城乡划分代码编制规则
--北京、天津、上海、重庆 INSERT INTO dmb_city (CityCode,CityName,CityLevel,CityRemark) VALUES ('110100','市辖区',2,'city'); INSERT INTO dmb_city (CityCode,CityName,CityLevel,CityRemark) VALUES ('120100','市辖区',2,'city'); INSERT INTO dmb_city (CityCode,CityName,CityLevel,CityRemark) VALUES ('310100','市辖区',2,'city'); INSERT INTO dmb_city (CityCode,CityName,CityLevel,CityRemark) VALUES ('500100','市辖区',2,'city'); -- 省 province -- 市 city -- 区 area -- 街道 town select * from dmb_city where CityCode like '%00' and CityLevel<>1 --市 update dmb_city set CityLevel=1,CityRemark='province' where where CityCode like '%0000' update dmb_city set CityLevel=2,CityRemark='city' where where CityCode like '%00' update dmb_city set CityLevel=3,CityRemark='area' where where CityLevel not in (1,2) --110000 --130200 select * from dmb_city where CityLevel=3 or CityLevel=1 --更新父级代码 update dmb_city set ParentCode=substring(CityCode,1,2)+'0000' where CityLevel=2 update dmb_city set ParentCode=substring(CityCode,1,4)+'00' where CityLevel=3 update dmb_city set ParentCode=substring(CityCode,1,6) where CityLevel=4 update dmb_city set CityName=substring(CityName,1,len(CityName)-1) where CityName like '%街道办'
#region 根据区获取街道信息 WebClient wc = new WebClient(); Stopwatch sw = new Stopwatch(); sw.Start(); Response.Write("<p>-------------------- 获取数据 begin --------------------</p>"); long index = 0; StringBuilder sb = new StringBuilder(); sb.Append("INSERT INTO dmb_city (CityCode,CityName) VALUES "); DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connectionString, CommandType.Text, "select * from dmb_city where CityLevel=3").Tables[0]; foreach (DataRow dr in dt.Rows) { //Response.Write("<p>" + dr["CityCode"] + "-" + dr["CityName"]+"</p>"); try { //http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/44/05/440511.html //拼接网址 string url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/" + dr["CityCode"].ToString().Substring(0, 2) + "/" + dr["CityCode"].ToString().Substring(2, 2) + "/" + dr["CityCode"].ToString() + ".html"; string html = wc.DownloadString(url); MatchCollection mc = Regex.Matches(html, "<tr\\sclass='towntr'><td><a\\shref='.+?'>(.+?)</a></td><td><a\\shref='.+?'>(.+?)</a></td></tr>"); int i = 0; //遍历匹配的内容 foreach (Match item in mc) { if (item.Success) { //Response.Write(item.Groups[1].Value); //拼接SQL 过滤无效字符 sb.Append("('" + item.Groups[1].Value.Substring(0, 9) + "','" + item.Groups[2].Value.Replace("办事处", "").Replace("街道办", "街道") + "')"); index++; if (index > 900) { sb.Append(";"); index = 1; sb.Append("INSERT INTO dmb_city (CityCode,CityName) VALUES "); } else { sb.Append(","); } } } } catch { //获取不到网络链接直接跳过 continue; } } Response.Write("<p>" + sb.ToString().TrimEnd(',') + ";</p>"); Response.Write("<p>-------------------- 获取数据 end --------------------</p>"); sw.Stop(); Response.Write("<p>-------------------- 共用 " + sw.ElapsedMilliseconds + "毫秒 --------------------</p>"); #endregion