How to 'union' 2 or more DataTables in C#?
2013-12-23 22:27
330 查看
down voteaccepted | You are looking most likely for the DataTable.Merge method. Example: 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(); } } |
相关文章推荐
- how to: pin or unpin from win7 taskbar in C#
- MySQL的or/in/union与索引优化
- two or more web modules defined in the configuration have the same context root
- two or more web modules defined in the configuration have the same context root
- HOW TO: Set a Windows Hook in Visual C# .NET
- Two or more Web modules defined in the configuration have the same context root (/bms).
- svn冲突:skipped remains conflicted one or more files are in a state.
- ataTable 去除重复项 HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- More Effective C# Item 7. Do Not Create Generic Specialization on Base Classes or Interfaces
- two or more data types in declaration of `main'
- One or more listeners failed to start. Full details will be found in the appropriate container log
- mysql 实战 or、in与union all 的查询效率
- 编译时报错: error: <strong>two or more data types in declaration specifiers</strong> error:two or more
- HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- two or more data types in declaration of 错误
- SVN:One or more files are in a conflicted state
- two or more data types in declaration of `函数名'的报错
- How to: Create a C# Test Project Add-In -wisdom
- error: two or more data types in declaration specifiers原因及解决方法
- error: two or more data types in declaration of 'main'