两个DataTable连接实例

static void Main(string[] args)
        {
            DataTable dt = GetData(); //组织父表数据

            DataTable dtDetail = GetDetailData();//组织子表数据

            try
            {
                string parentFieldName = "ID";
                string relationFieldName = "d_ParentID";
                DataTable joinDt = JoinData(dt, dtDetail, parentFieldName, relationFieldName, false);// 合并表数据
                Console.Write("sucess");
            }
            catch (Exception ex)
            { }
        }

 

        //组织父表数据

        private static DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", Type.GetType("System.Int32")));
            dt.Columns.Add(new DataColumn("Code", Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));

            DataRow dr = null;
            for (int i = 0; i 10; i++)
            {
                dr = dt.NewRow();
                dr["ID"] = i;
                dr["Code"] = i;
                dr["Name"] = i;
                dt.Rows.Add(dr);
            }
            return dt;
        }

 

        //组织子表数据

        private static DataTable GetDetailData()
        {
            DataTable dtDetail = new DataTable();
            dtDetail.Columns.Add(new DataColumn("d_ID", Type.GetType("System.Int32")));
            dtDetail.Columns.Add(new DataColumn("d_ParentID", Type.GetType("System.Int32")));
            dtDetail.Columns.Add(new DataColumn("d_Code", Type.GetType("System.String")));
            dtDetail.Columns.Add(new DataColumn("d_Name", Type.GetType("System.String")));
            DataRow dr = null;
            for (int i = 0; i 5; i++)
            {
                dr = dtDetail.NewRow();
                dr["d_ID"] = 2 * i;
                dr["d_ParentID"] = 2 * i;
                dr["d_Code"] = 2 * i;
                dr["d_Name"] = 2 * i;
                dtDetail.Rows.Add(dr);
            }

            return dtDetail;
        }

 

        /// summary
        /// 合并表数据
        /// /summary
        /// param name="dt"合并主表/param
        /// param name="dtDetail"合并明细表/param
        /// param name="isInnerJoin"是否内联/param
        /// returnsDataTable/returns
        private static DataTable JoinData(DataTable dt, DataTable dtDetail, string parentFieldName, string relationFieldName, bool isInnerJoin)
        {
            DataTable joinDt = new DataTable();

            try
            {
                using (DataSet ds = new DataSet())
                {
                    ds.Tables.AddRange(new DataTable[] { dt, dtDetail });

                    DataRelation relation = new DataRelation("MasterRelation", dt.Columns[parentFieldName], dtDetail.Columns[relationFieldName], 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;
        }

 

最新回复(0)
/jishucTU0lsKRglHpwPy_2Bh1Q3BeD83td1ZINbWtD4WeWYVH8_3D4652173
8 简首页