当前位置:网站首页>将两个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();
}
}
边栏推荐
- MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
- JNI基本使用
- 大龄程序员的心理建设
- Summary of MySQL database interview questions (2022 latest version)
- php将多维数据保存进json文件
- npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
- CentOS7 - yum install mysql
- 嵌入式系统驱动初级【3】——字符设备驱动基础中_IO模型
- 基于C语言的学生信息管理系统_(更新版)_(附源码和安装包)_课程设计_**往事随風**的博客
- Delphi-C端有趣的菜单操作界面设计
猜你喜欢

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

关于C#的反射,你真的运用自如嘛?

Delphi-C端有趣的菜单操作界面设计

Sublime Text 3 2021.8.3 个人配置

Grain Mall - Basics (Project Introduction & Project Construction)

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

webrtc中视频采集实现分析(二) 视频帧的分发

npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa

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

进入古诗文网站个人中心,绕过登录
随机推荐
梳理CamStyle、PTGAN、SPGAN、StarGAN
C语言 -- 操作符详解
(Kettle) pdi-ce-8.2 连接MySQL8.x数据库时驱动问题之终极探讨及解决方法分析
7.15 Day21---MySQL----索引
显式调用类的构造函数(而不是用赋值构造),实现一个new操作
12、分页插件
Cannot read properties of null (reading 'insertBefore')
想低成本保障软件安全?5大安全任务值得考虑
webrtc中的任务队列TaskQueue
7、特殊SQL的执行
Programming hodgepodge (3)
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
MySQL数据库(基础)
嵌入式系统驱动初级【3】——字符设备驱动基础中_IO模型
warning C4251: “std::vector&lt;_Ty&gt;”需要有 dll 接口由 class“Test”的客户端使用错误
7.16 Day22---MYSQL (Dao mode encapsulates JDBC)
FPGA学习笔记——知识点总结
关于let var 和const的区别以及使用
手把手教你实现buffer(二)——内存管理及移动语义
Swoole学习(二)