ASP.NET工具类-合并DataTable


using System.Data;
using System;
public class DataTableMerge
{
    #region 多列连接
    /// <summary>
    /// 合并两个dataTable 类似 select * from  tableA a,tableB b where a.id=b.id
    /// </summary>
    /// <param name="First">第一个dataTable</param>
    /// <param name="Second">第二个dataTable</param>
    /// <param name="FJC">第一个的连接列</param>
    /// <param name="SJC">第二个的连接列</param>
    /// <returns></returns>
    public static DataTable Join(DataTable First, DataTable Second, DataColumn[] FJC, DataColumn[] SJC)
    {

        //创建一个新的DataTable  

        DataTable table = new DataTable("Join");


        // Use a DataSet to leverage DataRelation  

        using (DataSet ds = new DataSet())
        {

            //把DataTable Copy到DataSet中  

            ds.Tables.AddRange(new DataTable[] { First.Copy(), Second.Copy() });

            DataColumn[] parentcolumns = new DataColumn[FJC.Length];

            for (int i = 0; i < parentcolumns.Length; i++)
            {

                parentcolumns  = ds.Tables[0].Columns[FJC.ColumnName];

            }

            DataColumn[] childcolumns = new DataColumn[SJC.Length];

            for (int i = 0; i < childcolumns.Length; i++)
            {

                childcolumns = ds.Tables[1].Columns[SJC.ColumnName];

            }


            //创建关联  

            DataRelation r = new DataRelation(string.Empty, parentcolumns, childcolumns, false);

            ds.Relations.Add(r);


            //为关联表创建列  

            for (int i = 0; i < First.Columns.Count; i++)
            {

                table.Columns.Add(First.Columns.ColumnName, First.Columns.DataType);

            }

            for (int i = 0; i < Second.Columns.Count; i++)
            {

                //看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second  

                if (!table.Columns.Contains(Second.Columns.ColumnName))

                    table.Columns.Add(Second.Columns.ColumnName, Second.Columns.DataType);

                else

                    table.Columns.Add(Second.Columns.ColumnName + "_Second", Second.Columns.DataType);

            }


            table.BeginLoadData();

            foreach (DataRow firstrow in ds.Tables[0].Rows)
            {

                //得到行的数据  

                DataRow[] childrows = firstrow.GetChildRows(r);

                if (childrows != null && childrows.Length > 0)
                {

                    object[] parentarray = firstrow.ItemArray;

                    foreach (DataRow secondrow in childrows)
                    {

                        object[] secondarray = secondrow.ItemArray;

                        object[] joinarray = new object[parentarray.Length + secondarray.Length];

                        Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);

                        Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);

                        table.LoadDataRow(joinarray, true);

                    }

                }

            }

            table.EndLoadData();

        }


        return table;

    }
    #endregion

    #region 单列连接
    /// <summary>
    /// 合并两个dataTable 类似 select * from  tableA a,tableB b where a.id=b.id
    ///更多http://www.52mvc.com/showtopic-1264.aspx
    /// </summary>
    /// <param name="First">第一个dataTable</param>
    /// <param name="Second">第二个dataTable</param>
    /// <param name="FJC">第一个的连接列</param>
    /// <param name="SJC">第二个的连接列</param>
    /// <returns></returns>
    public static DataTable Join(DataTable First, DataTable Second, DataColumn FJC, DataColumn SJC)
    {
        return Join(First, Second, new DataColumn[] { FJC }, new DataColumn[] { SJC });
    }

    /// <summary>
    /// 合并两个dataTable 类似 select * from  tableA a,tableB b where a.id=b.id
    /// </summary>
    /// <param name="First">第一个dataTable</param>
    /// <param name="Second">第二个dataTable</param>
    /// <param name="FJC">第一个的连接列</param>
    /// <param name="SJC">第二个的连接列</param>
    /// <returns></returns>
    public static DataTable Join(DataTable First, DataTable Second, string FJC, string SJC)
    {

        return Join(First, Second, new DataColumn[] { First.Columns[FJC] }, new DataColumn[] { First.Columns[SJC] });

    }
    #endregion
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值