1 /// <summary> 2 /// 将DataTable集合 B加入到DataTable A中,应用于两个或以上的只有单行数据的无主键的datatable的合并 3 /// </summary> 4 /// <param name="ArrOrigeon">A</param> 5 /// <param name="ArrAdded">B</param> 6 /// <returns>合并后的DataTable</returns> 7 public static sysDataTable Union(this sysDataTable tbOrigeon, params sysDataTable[] tbAdded) 8 { 9 sysDataTable arrs = new sysDataTable(); 10 //向tbOrigeon表增加主键 11 tbOrigeon.Columns.Add("id", typeof(int)); 12 tbOrigeon.Rows[0]["id"] = 1; 13 tbOrigeon.PrimaryKey = new System.Data.DataColumn[]{tbOrigeon.Columns["id"]}; 14 arrs = tbOrigeon; 15 foreach (sysDataTable dt in tbAdded) 16 { 17 if (dt.Columns.Count > 0) 18 { 19 //向dt表增加主键 20 dt.Columns.Add("id", typeof(int)); 21 dt.Rows[0]["id"] = 1; 22 dt.PrimaryKey = new System.Data.DataColumn[] { dt.Columns["id"] }; 23 //合并表 24 arrs.Merge(dt, false); 25 } 26 } 27 //将主键置为空 28 arrs.PrimaryKey = null; 29 //删除临时主键 30 arrs.Columns.Remove("id"); 31 return arrs; 32 }
时间: 2024-10-12 14:59:29