using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace 省市查询

{
class Program

{
static void Main(
string[] args)

{
string connString = ConfigurationManager.ConnectionStrings[
"ConString"].ConnectionString;
//获取配置文件App.config的连接字符串
using (SqlConnection conn =
new SqlConnection(connString))

{

SqlDataAdapter thisDataAdapter =
new SqlDataAdapter(
"select proID,proName from promary",conn);
//建立Adapter对象

SqlCommandBuilder thisBuild =
new SqlCommandBuilder(thisDataAdapter);
//建立commandBuild对象

DataSet thisDataSet =
new DataSet();
//创建DataSet
//创建sqlDataAdapter来填充thisDataSet

SqlDataAdapter proAdapter =
new SqlDataAdapter(
"select * from promary",conn);

SqlDataAdapter cityAdapter =
new SqlDataAdapter(
"select * from city",conn);

proAdapter.Fill(thisDataSet,
"promary");

cityAdapter.Fill(thisDataSet,
"city");

DataRelation promcity = thisDataSet.Relations.Add(
"promcity", thisDataSet.Tables[
"promary"].Columns[
"proID"],thisDataSet.Tables[
"city"].Columns[
"proID"]);
foreach (DataRow proRow
in thisDataSet.Tables[
"promary"].Rows)

{

Console.WriteLine(
"省/地区:"+proRow[
"proName"]);
foreach (DataRow city
in proRow.GetChildRows(promcity))

{

Console.WriteLine(
" 市级:"+city[
"cityName"]);

}

}

Console.ReadLine();

}

}

}

}