当前位置:网站首页>将两个DataTable合并——DataTable.Merge 方法
将两个DataTable合并——DataTable.Merge 方法
2022-08-04 05:26:00 【qq_26695613】

private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable("Items");
// Add columns
DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));
table1.Columns.Add(idColumn);
table1.Columns.Add(itemColumn);
// Set the primary key column.
table1.PrimaryKey = new DataColumn[] { idColumn };
// Add RowChanged event handler for the table.
table1.RowChanged += new
System.Data.DataRowChangeEventHandler(Row_Changed);
// Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = table1.NewRow();
row["id"] = i;
row["item"] = i;
table1.Rows.Add(row);
}
// Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values");
// Create a second DataTable identical to the first.
DataTable table2 = table1.Clone();
// Add column to the second column, so that the
// schemas no longer match.
table2.Columns.Add("newColumn", typeof(System.String));
// Add three rows. Note that the id column can't be the
// same as existing rows in the original table.
row = table2.NewRow();
row["id"] = 14;
row["item"] = 774;
row["newColumn"] = "new column 1";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
row["newColumn"] = "new column 2";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
row["newColumn"] = "new column 3";
table2.Rows.Add(row);
// Merge table2 into the table1.
Console.WriteLine("Merging");
table1.Merge(table2, false, MissingSchemaAction.Add);
PrintValues(table1, "Merged With table1, schema added");
}
private static void Row_Changed(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine("Row changed {0}\t{1}", e.Action,
e.Row.ItemArray[0]);
}
private static void PrintValues(DataTable table, string label)
{
// Display the values in the supplied DataTable:
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.Write("\t " + row[col].ToString());
}
Console.WriteLine();
}
}
边栏推荐
猜你喜欢
随机推荐
即时通讯网 即时通讯音视频开发
动态规划总括
跨域问题的解决
npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
力扣:746. 使用最小花费爬楼梯
程序员的财富观
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
webrtc中的视频编码(一) 视频编码模块轮廓
ORACLE LINUX 6.5 安装重启后Kernel panic - not syncing : Fatal exception
擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
DP4398:国产兼容替代CS4398立体声24位/192kHz音频解码芯片
JS基础--强制类型转换(易错点,自用)
通过&修改数组中的值
【问题解决】同一机器上Flask部署TensorRT报错记录
Can 't connect to MySQL server on' localhost3306 '(10061) simple solutions
FLV格式详解
Swoole学习(二)
7.13 Day20----MYSQL
Grain Mall - Basics (Project Introduction & Project Construction)
自动化测试的成本高效果差,那么自动化测试的意义在哪呢?









