DataSet.Merge 方法
将此 DataSet 与指定的 DataSet 合并。
private void DemonstrateMergeTableAddSchema(){
// Create a DataSet with one table, two columns, and ten rows.
DataSet ds = new DataSet("myDataSet");
DataTable t = new DataTable("Items");
// Add table to the DataSet
ds.Tables.Add(t);
// Create and add two columns to the DataTable
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"),"");
c1.AutoIncrement=true;
DataColumn c2 = new DataColumn("Item", Type.GetType("System.Int32"),"");
t.Columns.Add(c1);
t.Columns.Add(c2);
// Set the primary key to the first column.
t.PrimaryKey = new DataColumn[1]{ c1 };
// Add RowChanged event handler for the table.
t.RowChanged+= new DataRowChangeEventHandler(Row_Changed);
// Add ten rows.
for(int i = 0; i <10;i++){
DataRow r=t.NewRow();
r["Item"]= i;
t.Rows.Add(r);
}
// Create a second DataTable identical to the first, with
// one extra column using the Clone method.
DataTable t2 = t.Clone();
t2.Columns.Add("extra", typeof(string));
// Add two rows. Note that the id column can't be the
// same as existing rows in the DataSet table.
DataRow newRow;
newRow=t2.NewRow();
newRow["id"]= 12;
newRow["Item"]=555;
newRow["extra"]= "extra Column 1";
t2.Rows.Add(newRow);
// Merge the table into the DataSet.
Console.WriteLine("Merging");
ds.Merge(t2,false,MissingSchemaAction.Add);
PrintValues(ds, "Merged With Table, Schema Added");
}