Last active
August 29, 2015 14:01
-
-
Save shengoo/c39baea4b105f72d43fd to your computer and use it in GitHub Desktop.
Merge datatable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //with same primary key, and same key value, they will combine | |
| public void testmerge() | |
| { | |
| DataTable table1 = new DataTable(); | |
| // Add columns | |
| DataColumn idColumn = new DataColumn("id", typeof(System.Int32)); | |
| DataColumn itemColumn = new DataColumn("item", typeof(System.String)); | |
| table1.Columns.Add(idColumn); | |
| table1.Columns.Add(itemColumn); | |
| // Set the primary key column. | |
| table1.PrimaryKey = new DataColumn[] { idColumn }; | |
| // 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); | |
| } | |
| DataTable table2 = new DataTable(); | |
| // Add columns | |
| DataColumn idColumn2 = new DataColumn("id", typeof(System.Int32)); | |
| DataColumn itemColumn2 = new DataColumn("item", typeof(System.String)); | |
| DataColumn nameColumn = new DataColumn("name", typeof(System.String)); | |
| table2.Columns.Add(idColumn2); | |
| table2.Columns.Add(nameColumn); | |
| table2.Columns.Add(itemColumn2); | |
| // Set the primary key column. | |
| table2.PrimaryKey = new DataColumn[] { idColumn2 }; | |
| // Add ten rows. | |
| DataRow row2; | |
| for (int i = 0; i <= 9; i++) | |
| { | |
| row = table2.NewRow(); | |
| row["id"] = i; | |
| row["name"] = "name" + i; | |
| row["item"] = "item" + i; | |
| table2.Rows.Add(row); | |
| } | |
| table1.Merge(table2); | |
| PrintValues(table1, "table1"); | |
| } | |
| 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(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment