当前位置:网站首页>将两个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();
}
}
边栏推荐
猜你喜欢

自己学习爬虫写的基础小函数

7.16 Day22---MYSQL (Dao mode encapsulates JDBC)

C语言 -- 操作符详解

MySQL数据库面试题总结(2022最新版)

【Matlab仿真】:一带电量为q的电荷以速度v运动,求运动电荷产生磁感应强度

FPGA学习笔记——知识点总结

MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log

DP4398:国产兼容替代CS4398立体声24位/192kHz音频解码芯片

想低成本保障软件安全?5大安全任务值得考虑

实际开发中左菜单自定义图标点击切换
随机推荐
Web Basics and Exercises for C1 Certification - My Study Notes
php实现telnet访问端口
Cannot read properties of null (reading 'insertBefore')
MySQL date functions
【Matlab仿真】:一带电量为q的电荷以速度v运动,求运动电荷产生磁感应强度
如何低成本修bug?测试左移给你答案
自己学习爬虫写的基础小函数
LCP 17. 速算机器人
FLV格式详解
MySql数据恢复方法个人总结
JS basics - forced type conversion (error-prone, self-use)
C1认证之web基础知识及习题——我的学习笔记
4.2 声明式事务概念
bind和function
8.03 Day34---BaseMapper query statement usage
关于事件捕获和事件冒泡的顺序,以及如何处理事件冒泡带来的影响
实际开发中左菜单自定义图标点击切换
关于 for in与for of 的差别以及如何使用
Swoole学习(一)
Handling List