#region 合并DataTable
private DataTable UniteDataTable(DataTable dt1, DataTable dt2, string DTName)
{
DataTable dt3 = dt1.Clone();
foreach (DataColumn q in dt2.Columns)
{
foreach (DataColumn v in dt1.Columns)
{
if (q.ColumnName == v.ColumnName)
{
q.ColumnName = v.ColumnName + "2";
}
}
dt3.Columns.Add(q.ColumnName);
}
object[] obj = new object[dt3.Columns.Count];
for (int i = 0; i < dt1.Rows.Count; i++)
{
dt1.Rows[i].ItemArray.CopyTo(obj, 0);
dt3.Rows.Add(obj);
}
if (dt1.Rows.Count >= dt2.Rows.Count)
{
for (int i = 0; i < dt2.Rows.Count; i++)
{
for (int j = 0; j < dt2.Columns.Count; j++)
{
dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();
}
}
}
else
{
DataRow dr3;
for (int i = 0; i < dt2.Rows.Count - dt1.Rows.Count; i++)
{
dr3 = dt3.NewRow();
dt3.Rows.Add(dr3);
}
for (int i = 0; i < dt2.Rows.Count; i++)
{
for (int j = 0; j < dt2.Columns.Count; j++)
{
dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();
}
}
}
dt3.TableName = DTName;
return dt3;
}
#endregion
C# 合并两个不同的DataTable
最新推荐文章于 2023-04-21 14:46:21 发布