ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 实现两个DataTable的联合查询[转]

实现两个DataTable的联合查询[转]

原创 Linux操作系统 作者:kirinri 时间:2009-06-07 21:51:58 0 删除 编辑
实现两个DataTable的联合查询
2008-08-18 09:22

      不用任何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;

        }

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

方法二:

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

/// 要合并的表一
        /// 要合并的表二
        /// dt1与dt2联系的关键列名

        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                {
                    if( dt1.Columns[i].ColumnName == KeyColName )
                    {
                        colKey1=i;
                    }

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

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

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

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


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

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

            return dtReturn ;
        }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12476590/viewspace-605397/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 关关难过 关关过
请登录后发表评论 登录
全部评论

注册时间:2007-12-21

  • 博文量
    257
  • 访问量
    501384