C#中串連兩個DataTable,相當於Sql的InnerJoin

來源:互聯網
上載者:User

    在下面的例子中實現了3個Join方法,其目的是把兩個DataTable串連起來,相當於Sql的Inner Join方法,返回DataTable的所有列。
如果兩個DataTable中的DataColumn有重複的話,把第二個設定為ColumnName+"_Second",下面是代碼,希望對大家有所協助。
using System;
using System.Data;

namespace WindowsApplication1
{
    public class SQLOps
    {
        public SQLOps()
        {           
        }
        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[i] = ds.Tables[0].Columns[FJC[i].ColumnName];
                }
                DataColumn[] childcolumns = new DataColumn[SJC.Length];
                for(int i = 0; i < childcolumns.Length; i++)
                {
                    childcolumns[i] = ds.Tables[1].Columns[SJC[i].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[i].ColumnName, First.Columns[i].DataType);
                }
                for(int i = 0; i < Second.Columns.Count; i++)
                {
                    //看看有沒有重複的列,如果有在第二個DataTable的Column的列明後加_Second
                    if(!table.Columns.Contains(Second.Columns[i].ColumnName))
                        table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
                    else
                        table.Columns.Add(Second.Columns[i].ColumnName + "_Second", Second.Columns[i].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;
        }
        public static DataTable Join (DataTable First, DataTable Second, DataColumn FJC, DataColumn SJC)
        {
            return Join(First, Second, new DataColumn[]{FJC}, new DataColumn[]{SJC});
        }
        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]});
        }
    }
}



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。