本文整理汇总了C#中System.Data.DataTable.Merge方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.Merge方法的具体用法?C# DataTable.Merge怎么用?C# DataTable.Merge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.DataTable
的用法示例。
在下文中一共展示了DataTable.Merge方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DemonstrateMergeTable
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();
}
}
示例2: DemonstrateMergeTable
private static void DemonstrateMergeTable()
{
DataTable itemsTable = new DataTable("Items");
// Add columns
DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));
itemsTable.Columns.Add(idColumn);
itemsTable.Columns.Add(itemColumn);
// Set the primary key column.
itemsTable.PrimaryKey = new DataColumn[] { idColumn };
// Add RowChanged event handler for the table.
itemsTable.RowChanged +=
new System.Data.DataRowChangeEventHandler(Row_Changed);
// Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = itemsTable.NewRow();
row["id"] = i;
row["item"] = i;
itemsTable.Rows.Add(row);
}
// Accept changes.
itemsTable.AcceptChanges();
PrintValues(itemsTable, "Original values");
// Create a second DataTable identical to the first.
DataTable itemsClone = itemsTable.Clone();
// Add column to the second column, so that the
// schemas no longer match.
itemsClone.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 = itemsClone.NewRow();
row["id"] = 14;
row["item"] = 774;
row["newColumn"] = "new column 1";
itemsClone.Rows.Add(row);
row = itemsClone.NewRow();
row["id"] = 12;
row["item"] = 555;
row["newColumn"] = "new column 2";
itemsClone.Rows.Add(row);
row = itemsClone.NewRow();
row["id"] = 13;
row["item"] = 665;
row["newColumn"] = "new column 3";
itemsClone.Rows.Add(row);
// Merge itemsClone into the itemsTable.
Console.WriteLine("Merging");
itemsTable.Merge(itemsClone, false, MissingSchemaAction.Add);
PrintValues(itemsTable, "Merged With itemsTable, 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();
}
}
示例3: DemonstrateMergeTable
private static void DemonstrateMergeTable()
{
// Demonstrate merging, within and without
// preserving changes.
// In this example, take these actions:
// 1. Create a DataTable (table1) and fill the table with data.
// 2. Create a copy of table1, and modify its data (modifiedTable).
// 3. Modify data in table1.
// 4. Make a copy of table1 (table1Copy).
// 5. Merge the data from modifiedTable into table1 and table1Copy,
// showing the difference between setting the preserveChanges
// parameter to true and false.
// Create a new DataTable.
DataTable table1 = new DataTable("Items");
// Add two columns to the table:
DataColumn column = new DataColumn("id", typeof(System.Int32));
column.AutoIncrement = true;
table1.Columns.Add(column);
column = new DataColumn("item", typeof(System.String));
table1.Columns.Add(column);
// Set primary key column.
table1.PrimaryKey = new DataColumn[] { table1.Columns[0] };
// Add some rows.
DataRow row;
for (int i = 0; i <= 3; i++)
{
row = table1.NewRow();
row["item"] = "Item " + i;
table1.Rows.Add(row);
}
// Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values");
// Using the same schema as the original table,
// modify the data for later merge.
DataTable modifiedTable = table1.Copy();
foreach (DataRow rowModified in modifiedTable.Rows)
{
rowModified["item"] = rowModified["item"].ToString()
+ " modified";
}
modifiedTable.AcceptChanges();
// Change row values, and add a new row:
table1.Rows[0]["item"] = "new Item 0";
table1.Rows[1]["item"] = "new Item 1";
row = table1.NewRow();
row["id"] = 4;
row["item"] = "Item 4";
table1.Rows.Add(row);
// Get a copy of the modified data:
DataTable table1Copy = table1.Copy();
PrintValues(table1, "Modified and new Values");
PrintValues(modifiedTable, "Data to be merged into table1");
// Merge new data into the modified data.
table1.Merge(modifiedTable, true);
PrintValues(table1, "Merged data (preserve changes)");
table1Copy.Merge(modifiedTable, false);
PrintValues(table1Copy, "Merged data (don't preserve changes)");
}
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 column in table.Columns)
{
Console.Write("\t{0}", row[column, DataRowVersion.Current]);
}
Console.WriteLine();
}
}
示例4: DemonstrateMergeTable
private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable("Items");
// Add columns
DataColumn column1 = new DataColumn("id", typeof(System.Int32));
DataColumn column2 = new DataColumn("item", typeof(System.Int32));
table1.Columns.Add(column1);
table1.Columns.Add(column2);
// Set the primary key column.
table1.PrimaryKey = new DataColumn[] { column1 };
// Add RowChanged event handler for the table.
table1.RowChanged +=
new System.Data.DataRowChangeEventHandler(Row_Changed);
// Add some rows.
DataRow row;
for (int i = 0; i <= 3; 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 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;
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
table2.Rows.Add(row);
// Merge table2 into the table1.
Console.WriteLine("Merging");
table1.Merge(table2);
PrintValues(table1, "Merged With table1");
}
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();
}
}