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

CentOS7 —— yum安装mysql

想好了吗?

TensorRT例程解读之语义分割demo
![Embedded system driver primary [3] - _IO model in character device driver foundation](/img/c7/21fc0651964a6a435e8ec5743b7662.png)
Embedded system driver primary [3] - _IO model in character device driver foundation

解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题

4.3 基于注解的声明式事务和基于XML的声明式事务

Cannot read properties of null (reading 'insertBefore')

7.16 Day22---MYSQL(Dao模式封装JDBC)

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

scrapy 爬取当当图书名字图片
随机推荐
乱码解决方案
Unity行为树AI分享
显式调用类的构造函数(而不是用赋值构造),实现一个new操作
JS基础--强制类型转换(易错点,自用)
Wwise入门和实战
Handling List
力扣:343. 整数拆分
Unity Visual Effect Graph入门与实践
ORACLE LINUX 6.5 安装重启后Kernel panic - not syncing : Fatal exception
8.03 Day34---BaseMapper query statement usage
Deploy LVS-DR cluster [experimental]
php实现telnet访问端口
LCP 17. 速算机器人
基于C语言的学生信息管理系统_(更新版)_(附源码和安装包)_课程设计_**往事随風**的博客
音视频相关基础知识与FFmpeg介绍
大龄程序员的心理建设
CentOS7 —— yum安装mysql
MySQL数据库面试题总结(2022最新版)
9. Dynamic SQL
warning C4251: “std::vector&lt;_Ty&gt;”需要有 dll 接口由 class“Test”的客户端使用错误