当前位置:网站首页>将两个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();
}
}
边栏推荐
猜你喜欢
随机推荐
The cost of automated testing is high and the effect is poor, so what is the significance of automated testing?
关于C#的反射,你真的运用自如嘛?
如何低成本修bug?测试左移给你答案
Embedded system driver primary [4] - under the basis of character device driver _ concurrency control
MySQL date functions
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
9、动态SQL
FLV格式详解
TensorRTx-YOLOv5工程解读(一)
C语言 -- 操作符详解
嵌入式系统驱动初级【3】——字符设备驱动基础中_IO模型
Linux环境下redis的下载、安装和启动(建议收藏)
实际开发中,客户要求密码输入框禁止粘贴~
Unity表格配置编辑工具
4.2 Declarative Transaction Concept
4.2 声明式事务概念
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
C language -- operator details
PHP实现异步执行程序
php实现telnet访问端口





![Deploy LVS-DR cluster [experimental]](/img/ad/84e05a6421d668b0b6ba6eeba0c730.jpg)



