|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Copying DataSet Contents
You can create a copy of aso 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.
Copy the schema, or relational structure, of the DataSet only, without copying any rows. Rows can be imported into an existingusing .
To create an exact copy of the DataSet that includes both schema and data, use themethod 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 themethod 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 themethod 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".
DataSet germanyCustomers = customerDataSet.Clone(); DataRow copyRows = customerDataSet.Tables["Customers"].Select("CountryRegion = 'Germany'"); DataTable customerTable = germanyCustomers.Tables["Customers"]; foreach (DataRow copyRow in copyRows) customerTable.ImportRow(copyRow);