实现两个DataTable的联合查询

原来联合查询的基础是这样的。

如方法一描述:将子表的数组追加到主表数组的下面。从而实现类似于视图(单表)的效果。

那么Left Join(Inner Join)和Right Join(Outer Join) 将如何实现呢?

明天仔细考虑下,看看有没有办法。

 不用任何sql语句的  

方法一:

public 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;

        }

////////////////////////////////////

方法二:

该方法更多体现的是两张表的合并

/// <param name="dt1">要合并的表一</param>
        /// <param name="dt2">要合并的表二</param>
        /// <param name="KeyColName">dt1与dt2联系的关键列名 </param>

        public DataTable MergeDataTable(DataTable dt1, DataTable dt2, String KeyColName)
        {
                //定义临时变量
                DataTable dtReturn = new DataTable();
                int i=0;
                int j=0;
                int k=0;
                int colKey1=0;
                int colKey2=0;


                //设定表dtReturn的名字
                dtReturn.TableName = dt1.TableName;
                //设定表dtReturn的列名
                for(i=0; i<dt1.Columns.Count; i++)
                {
                    if( dt1.Columns[i].ColumnName == KeyColName )
                    {
                        colKey1=i;
                    }

                    dtReturn.Columns.Add( dt1.Columns[i].ColumnName );
                }

                for(j=0; j<dt2.Columns.Count; j++)
                {
                    if( dt2.Columns[j].ColumnName == KeyColName )
                    {
                         colKey2=j;
                        continue;
                    }

               dtReturn.Columns.Add(dt2.Columns[j].ColumnName);
              }

                //建立表的空间
                for(i=0; i<dt1.Rows.Count; i++)
                {
                    DataRow dr;
                    dr = dtReturn.NewRow();
                    dtReturn.Rows.Add(dr);
                }


                //将表dt1,dt2的数据写入dtReturn
                for(i=0; i<dt1.Rows.Count; i++)
                {
                    int m=-1;
                    //表dt1的第i行数据拷贝到dtReturn中去
                    for(j=0; j<dt1.Columns.Count; j++)
                    {
                        dtReturn.Rows[i][j] = dt1.Rows[i][j].ToString();
                    }

                //查找的dt2中KeyColName的数据,与dt1相同的行
                    for(k=0; k<dt2.Rows.Count; k++)
                    {
                        if( dt1.Rows[i][colKey1].ToString() == dt2.Rows[k][colKey2].ToString() )
                        {
                            m=k;
                        }
                    }
                   
                    //表dt2的第m行数据拷贝到dtReturn中去,且不要KeyColName(ID)列
                    if( m!=-1 )
                    {
                        for(k=0; k<dt2.Columns.Count; k++)
                        {
                            if( k==colKey2 )
                            {
                                continue;
                            }
                            dtReturn.Rows[i][j] = dt2.Rows[m][k].ToString();
                            j++;
                        }
                    }
                }

            return dtReturn ;
        }

你可能感兴趣的:(JOIN,object,String,table,null,dataset)