DataTable表连接
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表连接的更多相关文章
- 把Linq查询返回的var类型的数据 转换为DataTable EF连接查询
问题:我要获得一个角色下对应的所有用户,需要两表连接查询,虽然返回的只有用户数据,但是我想到若是返回的不只是用户数据,而还要加上角色信息,那么我返回什么类型呢,返回var吗,这样不行. 于是我网上找找 ...
- SQL多表连接查询(详细实例)
转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...
- 关于Oracle表连接
表连接注意left join on与where的区别: select * from dept; select * from emp; select * from emp a right outer j ...
- SQL多表连接查询
SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student 截图如下: 表2:course 截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...
- oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图
多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所 ...
- Access数据库多表连接查询
第一次在Access中写多表查询,就按照MS数据库中的写法,结果报语法错,原来Access的多表连接查询是不一样的 表A.B.C,A关联B,B关联C,均用ID键关联 一般写法:select * fro ...
- PostgreSQL-join多表连接查询和子查询
一.多表连接查询 1.连接方式概览 [inner] join 内连接:表A和表B以元组为单位做一个笛卡尔积,记为表C,然后在C中挑选出满足符合on 语句后边的限制条件的内容. left [outer] ...
- SQL 中不同类型的表连接
http://www.linuxidc.com/Linux/2012-08/68035.htm 1.简介 在关系型数据库中,join操作是将不同的表中的数据联合在一起时非常通用的一种做法.首先让我们看 ...
- SQL表连接查询(inner join、full join、left join、right join)
SQL表连接查询(inner join.full join.left join.right join) 前提条件:假设有两个表,一个是学生表,一个是学生成绩表. 表的数据有: 一.内连接-inner ...
随机推荐
- Light Probe Proxy Volume
[Light Probe Proxy Volume] The Light Probe Proxy Volume (LPPV) component allows you to use more ligh ...
- Java 管道PipedInputStream PipedOutStream PipedReader PipedWriter
java中的管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据.一个线程发送数据到输出管道,另外一个线程从输入管道中读取数据.通过使用管道,实现不同线程间的通信,而不必借助类似 ...
- mysql不能使用IP连接,可以使用localhost连接
问题: 本地mysql,使用127.0.0.1可以连接成功,使用具体IP连接报错 ERROR 1130 (HY000): Host '10.252.225.125' is not allowed to ...
- js常用返回网页顶部几种方法
一.使用锚标记 此方法最简单,只需在body下放个隐藏的锚点标记,内容如下: 代码如下 复制代码 <a name="top" id="top">& ...
- 全国高校绿色计算大赛 预赛第一阶段(C++)第1关:将字符串反转
挑战任务 将输入的字符串str进行反转. 编程要求 补全右侧vector<char> inversion(string str)函数实现字符串的反转并返回反转之后的字符串,其中函数参数st ...
- Centos 7 下 LAMP 部署
一.介绍 LAMP is a combination of operating system and open-source software stack. The acronym of LAMP i ...
- Bar-Code-Recognition-System Private
本设计研究EAN13条形码识别的译码技术,在基于机器视觉技术上,构建了一套条形码在线检测识别系统,系统将由以下几个部分构成:条形码图像定位与采集.计算机图像预处理.条形码图像中值滤波去噪.条码解析与识 ...
- python网络爬虫《爬取get请求的页面数据》
一.urllib库 urllib是python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在python3中的为urllib.request和urllib. ...
- swift - label 的font 设置 文字字体和大小
设置字体和颜色 lab.textColor = UIColor.init(hexColor: "795928") lab.font = UIFont.systemFont(ofSi ...
- fastjson的常用方法
/** * 解析对象形式的json字符串 */ public static void test1() { String jsonStr = "{\"JACKIE_ZHANG\&qu ...