zoukankan      html  css  js  c++  java
  • 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 = 0; i < dt.Columns.Count; i++)
                        {
                            joinDt.Columns.Add(dt.Columns[i].ColumnName, dt.Columns[i].DataType);
                        }
                        for (int i = 0; i < dtDetail.Columns.Count; i++)
                        {
                            joinDt.Columns.Add(dtDetail.Columns[i].ColumnName, dtDetail.Columns[i].DataType);
                        }
    
    
                        joinDt.BeginLoadData();
                        foreach (DataRow firstrow in ds.Tables[0].Rows)
                        {
                            //得到行的数据
                            DataRow[] childrows = firstrow.GetChildRows(relation);
                            object[] parentarray = firstrow.ItemArray;
                            if (childrows != null && childrows.Length > 0)
                            {
                                foreach (DataRow childrow in childrows)
                                {
                                    object[] childarray = childrow.ItemArray;
                                    object[] joinarray = new object[parentarray.Length + childarray.Length];
                                    Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
                                    Array.Copy(childarray, 0, 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;
            }
  • 相关阅读:
    python3安装crypto出错,及解决方法
    php中的引用
    算法
    HTTP协议
    jdk 1.8 InvocationHandler 中文注释
    Java实现多线程的几种方法
    shell编写显示ps相关脚本
    逆波兰表达式求值(后序表达式)
    155. 最小栈(leetcode简单题)
    字符串逆序
  • 原文地址:https://www.cnblogs.com/kexb/p/9236526.html
Copyright © 2011-2022 走看看