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

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

Unity行为树AI分享

利用Jenkins实现Unity自动化构建

Unity Visual Effect Graph入门与实践

CentOS7 - yum install mysql

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

Sublime Text 3 2021.8.3 个人配置

Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法

想好了吗?

7.18 Day23----标记语言
随机推荐
谷粒商城-基础篇(项目简介&项目搭建)
OpenCV获取和设置图像的平均亮度
EntityComponentSystemSamples学习笔记
TensorRTx-YOLOv5工程解读(一)
将自定义类型作为关联容器的key
Web Basics and Exercises for C1 Certification - My Study Notes
7.16 Day22---MYSQL(Dao模式封装JDBC)
通过&修改数组中的值
处理List<Map<String, String>>类型
webrtc中的引用计框架
嵌入式系统驱动初级【3】——字符设备驱动基础中_IO模型
7.15 Day21---MySQL----索引
注意!软件供应链安全挑战持续升级
基于C语言的学生信息管理系统_(更新版)_(附源码和安装包)_课程设计_**往事随風**的博客
OpenGLES 学习之帧缓存
Linux环境下redis的下载、安装和启动(建议收藏)
Programming hodgepodge (3)
Unity表格配置编辑工具
MySQL数据库面试题总结(2022最新版)
利用Jenkins实现Unity自动化构建