为dataset的多个表定义表间关系,并输出主从表
2009-12-10 15:57
183 查看
为dataset的多个表定义表间关系,并输出主从表:
// 创建dataset
string constr = "server=(local);uid=sa;pwd=123456; database=信息中心科技资料管理数据库";
SqlConnection con = new SqlConnection(constr);
string com1 = "select * from 资料信息表";
string com2 = "select * from 资料借阅表";
SqlDataAdapter ada1 = new SqlDataAdapter(com1,con);
SqlDataAdapter ada2 = new SqlDataAdapter(com2,con);
DataSet ds = new DataSet();
ada1.Fill(ds, "资料信息表");
ada2.Fill(ds, "资料借阅表");
//定义表间关系
ds.Relations.Add("关系名", ds.Tables["主表"].Columns["主表主键"], ds.Tables["从表"].Columns["从表外键"]);
//由主表找出从有并输出
foreach (DataRow perrow in ds.Tables["资料信息表"].Rows)
{
DataRow[] chilrow =perrow.GetChildRows("rel"); //"rel"关系名
int roomCount = chilrow.Length;
Console.WriteLine("档号 {0} 行数 {1} 借阅信息:{2}", perrow["档号"], roomCount,roomCount > 1 ? "--" : "");
foreach (DataRow theRoom in chilrow)
Console.WriteLine("借阅人: {0}", theRoom["借阅人"]);
}
//由从表找到主表并输出
foreach (DataRow theChild in ds.Tables["资料借阅表"].Rows)
{
DataRow[] parents = theChild.GetParentRows("rel");
foreach (DataRow thePar in parents)
Console.WriteLine("借阅人: {0} 被借阅的档号: {1}",
theChild["借阅人"],
thePar["档号"]);
}
// 创建dataset
string constr = "server=(local);uid=sa;pwd=123456; database=信息中心科技资料管理数据库";
SqlConnection con = new SqlConnection(constr);
string com1 = "select * from 资料信息表";
string com2 = "select * from 资料借阅表";
SqlDataAdapter ada1 = new SqlDataAdapter(com1,con);
SqlDataAdapter ada2 = new SqlDataAdapter(com2,con);
DataSet ds = new DataSet();
ada1.Fill(ds, "资料信息表");
ada2.Fill(ds, "资料借阅表");
//定义表间关系
ds.Relations.Add("关系名", ds.Tables["主表"].Columns["主表主键"], ds.Tables["从表"].Columns["从表外键"]);
//由主表找出从有并输出
foreach (DataRow perrow in ds.Tables["资料信息表"].Rows)
{
DataRow[] chilrow =perrow.GetChildRows("rel"); //"rel"关系名
int roomCount = chilrow.Length;
Console.WriteLine("档号 {0} 行数 {1} 借阅信息:{2}", perrow["档号"], roomCount,roomCount > 1 ? "--" : "");
foreach (DataRow theRoom in chilrow)
Console.WriteLine("借阅人: {0}", theRoom["借阅人"]);
}
//由从表找到主表并输出
foreach (DataRow theChild in ds.Tables["资料借阅表"].Rows)
{
DataRow[] parents = theChild.GetParentRows("rel");
foreach (DataRow thePar in parents)
Console.WriteLine("借阅人: {0} 被借阅的档号: {1}",
theChild["借阅人"],
thePar["档号"]);
}
相关文章推荐
- 小插曲--输出1900-2020的闰年年份(函数的定义与调用)
- 2017-2-17,c#基础,输入输出,定义变量,变量赋值,int.Parse的基础理解,在本的初学者也能看懂(未完待续)
- shell 定义数组,declare 输出数组,变量自加
- C语言不同进制的定义、输入、输出
- 进程和线程的定义、关系及区别联系
- BPEL元素定义层次关系
- Object/DataSet Relational Mapping(对象/数据集关系映射)完整版本下载
- TableAdapter、Dataset与BindingSource的关系
- Linux 利用宏和printf定义特殊的输出(转载)
- 定义一个整数N,不用本地变量和循环,输入N,依次判断2N,4N,8N...,一旦大于5000,则倒序输出小于5000的那些数(...,8N,4N,2N,N)
- 【教育】互联网模式下重新定义独立名师、培训机构和在线平台三者关系
- art-template 模板定义变量并更改变量值 不让其输出显示
- 读取多个table,存放到dataset,创建关系,导出树形XML格式
- asp.net如何将DataSet转换成josn并输出
- java小练习(使用八种基本数据类型定义八个变量,然后在控制台输出各个变量的值)
- 查找程序中的错误(平凡的程序,不平凡的问题) 4.不定义第三个变量,互换两个变量的值 5.char 类型转换 6.输出 10~100 之间的随机整数
- swift打印输出、变量和常量的定义和常用类型
- 定义一个int型数组,包含10个元素,分别赋值为1~10, 然后将数组中的元素都向前移一个位置,最后一个元素的值是原来第一个元素的值,然后输出这个数组
- 定义一个函数,函数有两个整型参数,函数的功能是计算并输出两个参数之间的奇数个数 和 偶数和。