C#的Datatable合并

  •  Datatable中表结构相同

需要合并的两个datatable表结构相同,将两个datatable中的每行数据都加入到新表中。

public DataTable NewItemArray2Sum(DataTable dt3, DataTable dt4)
        {           
           
            DataTable newtable = dt3.Copy();
            for (int i = 0; i < dt4.Rows.Count; i++)
            {
                newtable.Rows.Add(dt4.Rows[i].ItemArray);
            }
            return newtable;
        }
  •  两个表结构不同的表合并成一个表,表中不含相同的列名,直接按行进行合并
public DataTable NewItemArray2(DataTable dt3, DataTable dt4)
        {
            
            //表1结构添加到新表
            DataTable newtable = dt3.Clone();

            //表2结构添加到新表
            for (int i = 0; i < dt4.Columns.Count; i++)
            {
                newtable.Columns.Add(dt4.Columns[i].ColumnName);
            }
            //给新表添数据
            int count = 0;
            object[] value = new object[newtable.Columns.Count];
            if (dt3.Rows.Count > dt4.Rows.Count)
            {
                count = dt3.Rows.Count;
            }
            else
            {
                count = dt4.Rows.Count;
            }

            for (int i = 0; i < count; i++)
            {
                dt3.Rows[i].ItemArray.CopyTo(value, 0);
                dt4.Rows[i].ItemArray.CopyTo(value, dt3.Columns.Count);
                newtable.Rows.Add(value);
            }
         
            return newtable;
        }
  • 两个结构不同的表合并,将相同的字段相同值合并成一行,直接把所有行合并
//string[] columnNames存放两个表中相同的列名
  public DataTable UnitDatatable2(DataTable dt3, DataTable dt4, string[] columnNames)
        {
            #region 方法一
            //判断输入的表中哪个表拥有的columnNames的行多,保证dt3中的行数多。
            if (dt4.Rows.Count > dt3.Rows.Count)
            {
                DataTable jh = dt3.Copy();
                dt3 = dt4;
                dt4 = jh;
            }
            //将表按照列名重新排序
            for (int i = 0; i < columnNames.Length; i++)
            {
                dt3.Columns[columnNames[i]].SetOrdinal(i);
                dt4.Columns[columnNames[i]].SetOrdinal(i);
            }
            //表3结构添加到新表
            DataTable newtable = dt3.Copy();
            //表4结构添加到新表
            for (int i = columnNames.Length; i < dt4.Columns.Count; i++)
            {
                newtable.Columns.Add(dt4.Columns[i].ColumnName);
            }
            int dt4Count = newtable.Columns.Count - dt3.Columns.Count;
            if (dt4Count > 0)
            {
                for (int i = 0; i < dt3.Rows.Count; i++)
                {
                    string sql = "";
                    for (int j = 0; j < columnNames.Length; j++)
                    {
                        sql = sql + columnNames[j] + " = '" + dt3.Rows[i][columnNames[j]] + "'   ";
                        if (j < columnNames.Length - 1)
                        {
                            sql = sql + "  and  ";
                        }
                    }
                    DataRow[] drs = dt4.Select(sql);
                    if (drs.Length > 0)
                    {
                        for (int k = 0; k < dt4Count; k++)
                        {
                            newtable.Rows[i][dt3.Columns.Count + k] = drs[0][columnNames.Length + k];
                        }
                    }

                }
            }
       //给新表添加表4中的数据
            #endregion
            return newtable;
        }

 

你可能感兴趣的:(C#)