Copying DataSet Contents (ADO.NET)
You can create a copy of a DataSet so that you can work with data without affecting the original data, or work with a subset of the data from a DataSet. When copying a DataSet, you can:
Create an exact copy of the DataSet, including the schema, data, row state information, and row versions.
Create a DataSet that contains the schema of an existing DataSet, but only rows that have been modified. You can return all rows that have been modified, or specify a specific DataRowState. For more information about row states, see Row States and Row Versions (ADO.NET).
To create an exact copy of the DataSet that includes both schema and data, use the Copy method of the DataSet. The following code example shows how to create an exact copy of the DataSet.
To create a copy of a DataSet that includes schema and only the data representing Added, Modified, or Deleted rows, use the GetChanges method of the DataSet. You can also use GetChanges to return only rows with a specified row state by passing a DataRowState value when calling GetChanges. The following code example shows how to pass a DataRowState when calling GetChanges.
// Copy all changes. DataSet changeDataSet = customerDataSet.GetChanges(); // Copy only new rows. DataSet addedDataSet= customerDataSet.GetChanges(DataRowState.Added);
To create a copy of a DataSet that only includes schema, use the Clone method of the DataSet. You can also add existing rows to the cloned DataSet using the ImportRow method of the DataTable. ImportRow adds data, row state, and row version information to the specified table. Column values are added only where the column name matches and the data type is compatible.
The following code example creates a clone of a DataSet and then adds the rows from the original DataSet to the Customers table in the DataSet clone for customers where the CountryRegion column has the value "Germany".