public  static System.Data.DataTable TableJoin(System.Data.DataTable dt, System.Data.DataTable dtDetail, string[] parentFieldName, string[] relationFieldName, bool isInnerJoin, string relationName = "")
{
System.Data.DataTable joinDt = new System.Data.DataTable(); try
{
using (DataSet ds = new DataSet())
{
ds.Tables.AddRange(new System.Data.DataTable[] { dt, dtDetail });
if (string.IsNullOrEmpty(relationName))
{
relationName = Guid.NewGuid().ToString();
}
List<DataColumn> parentc = new List<DataColumn>();
List<DataColumn> childc = new List<DataColumn>();
foreach (var item in parentFieldName)
{
parentc.Add(dt.Columns[item]);
}
foreach (var item in relationFieldName)
{
childc.Add(dtDetail.Columns[item]);
}
DataRelation relation = new DataRelation(relationName, parentc.ToArray(), childc.ToArray(), false);
ds.Relations.Add(relation); for (int i = ; i < dt.Columns.Count; i++)
{
joinDt.Columns.Add(dt.Columns[i].ColumnName, dt.Columns[i].DataType);
}
for (int i = ; i < dtDetail.Columns.Count; i++)
{
joinDt.Columns.Add(dtDetail.Columns[i].ColumnName, dtDetail.Columns[i].DataType);
} joinDt.BeginLoadData();
foreach (DataRow firstrow in ds.Tables[].Rows)
{
//得到行的数据
DataRow[] childrows = firstrow.GetChildRows(relation);
object[] parentarray = firstrow.ItemArray;
if (childrows != null && childrows.Length > )
{
foreach (DataRow childrow in childrows)
{
object[] childarray = childrow.ItemArray;
object[] joinarray = new object[parentarray.Length + childarray.Length];
Array.Copy(parentarray, , joinarray, , parentarray.Length);
Array.Copy(childarray, , joinarray, parentarray.Length, childarray.Length);
joinDt.LoadDataRow(joinarray, true);
}
}
else
{
if (!isInnerJoin)
{
joinDt.LoadDataRow(parentarray, true);
}
}
}
joinDt.EndLoadData();
}
}
catch (Exception ex)
{
throw ex;
} return joinDt;
}

DataTable表连接的更多相关文章

  1. 把Linq查询返回的var类型的数据 转换为DataTable EF连接查询

    问题:我要获得一个角色下对应的所有用户,需要两表连接查询,虽然返回的只有用户数据,但是我想到若是返回的不只是用户数据,而还要加上角色信息,那么我返回什么类型呢,返回var吗,这样不行. 于是我网上找找 ...

  2. SQL多表连接查询(详细实例)

    转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...

  3. 关于Oracle表连接

    表连接注意left join on与where的区别: select * from dept; select * from emp; select * from emp a right outer j ...

  4. SQL多表连接查询

    SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...

  5. oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图

        多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所 ...

  6. Access数据库多表连接查询

    第一次在Access中写多表查询,就按照MS数据库中的写法,结果报语法错,原来Access的多表连接查询是不一样的 表A.B.C,A关联B,B关联C,均用ID键关联 一般写法:select * fro ...

  7. PostgreSQL-join多表连接查询和子查询

    一.多表连接查询 1.连接方式概览 [inner] join 内连接:表A和表B以元组为单位做一个笛卡尔积,记为表C,然后在C中挑选出满足符合on 语句后边的限制条件的内容. left [outer] ...

  8. SQL 中不同类型的表连接

    http://www.linuxidc.com/Linux/2012-08/68035.htm 1.简介 在关系型数据库中,join操作是将不同的表中的数据联合在一起时非常通用的一种做法.首先让我们看 ...

  9. SQL表连接查询(inner join、full join、left join、right join)

    SQL表连接查询(inner join.full join.left join.right join) 前提条件:假设有两个表,一个是学生表,一个是学生成绩表. 表的数据有: 一.内连接-inner ...

随机推荐

  1. Velocity Obstacle

    [Velocity Obstacle] Two circular objects A,B, at time t(0), with velocity V(A),V(B). A represent the ...

  2. fiddler常用操作

    fiddler常用操作 标签(空格分隔): fiddler fidrdler抓取https请求: fiddler是一个很好的抓包工具,但是默认的是抓取HTTP的,对于pc的https的会提示网页不安全 ...

  3. TZOJ 3659 神奇的探险之旅(有向无环每个点只能经过一次最长路dij)

    描述 我们正在设计这样的一款儿童探险游戏:游戏由很多故事场景组成,每个场景中都有一个问题,游戏根据玩家的回答将进入下一场景,经过巧妙的设计,我们保证在一次“探险旅行”中,不会重复的进入任何相同的场景, ...

  4. FortiGate基本信息

    1.介绍 FortiGate是全新的下一代防火墙,在整个硬件架构和系统上面都有新的设计,在性能和功能上面都有了很大提升,具有性能高.接口丰富.功能齐全.安全路由交换一体化.性价比高等优势. Forti ...

  5. 自编辑列的gridview,分页,删除,点击删除提示“确认”

    分页:    gridview的属性中:AllowPaging="True"  PageSize="2"    找到gridview的PageIndexChan ...

  6. [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. day 17 re模块

    RE模块 import re 对一个大篇幅的字符串,按照你的规则找出想要的字符串 # 单个字符匹配 import re # \w 与 \W #字母数字下划线, 非 # print(re.findall ...

  8. LINUX SSH修改默认22/添加端口

    通常ssh远程登录的默认端口是22,但是因为端口22属于高危端口,因此很多时候作为服务器会被关掉,不过这个端口一般是可以更改或者添加的,这样除了22端口,也可以通过别的端口进行访问. 1.首先修改配置 ...

  9. oracle学习之数据库数据保存成文件

    常常需要将数据库中的数据生成文档,由于比较喜欢脚本的方式,所以就需要使用spool的时候进行格式设置,以下简单整理了一下oracle中进行格式设置的一些东西,一共十八条,其实常用的也就那么几个,稍后会 ...

  10. bootstrap日历控件

    bootstrap的日历控件: <link href="~/bootstrap/css/bootstrap.min.css" rel="stylesheet&quo ...