C# 操作DataTable, 使用Merge()方法合并数据
using System;
using System.Data;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("id", typeof(int));
dt1.Columns.Add("title", typeof(string));
dt1.Columns["id"].AutoIncrement = true;
for (int i = 0; i < 10; ++i)
{
DataRow newRow = dt1.NewRow();
newRow["title"] = DateTime.Now.Ticks.ToString();
dt1.Rows.Add(newRow);
}
DataTable dt2 = new DataTable();
dt2.Columns.Add("id", typeof(int));
dt2.Columns.Add("title", typeof(string));
dt2.Columns["id"].AutoIncrement = true;
for (int i = 0; i < 10; i++)
{
DataRow newRow = dt2.NewRow();
newRow["title"] = DateTime.Now.Ticks.ToString();
dt2.Rows.Add(newRow);
}
// dt1合并dt2数据
dt1.Merge(dt2);
}
}
}