当前位置:网站首页>将两个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();
}
}
边栏推荐
猜你喜欢
Canal mysql data synchronization
SLSA 框架与软件供应链安全防护
记录获取参赛选手信息过程
ORACLE LINUX 6.5 安装重启后Kernel panic - not syncing : Fatal exception
关于C#的反射,你真的运用自如嘛?
Cannot read properties of null (reading 'insertBefore')
解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题
擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
Code Refactoring: For Unit Testing
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
随机推荐
箭头函数的使用
MySQL database (basic)
php实现telnet访问端口
关于let var 和const的区别以及使用
webrtc中视频采集实现分析(二) 视频帧的分发
想低成本保障软件安全?5大安全任务值得考虑
登录页面js手写
4.3 Annotation-based declarative transactions and XML-based declarative transactions
处理List<Map<String, String>>类型
FPGA学习笔记——知识点总结
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
PHP实现异步执行程序
Linux环境下redis的下载、安装和启动(建议收藏)
利用Jenkins实现Unity自动化构建
MySql数据恢复方法个人总结
Swoole学习(一)
如何低成本修bug?测试左移给你答案
8.03 Day34---BaseMapper查询语句用法
webrtc中的任务队列TaskQueue
Programming hodgepodge (3)