Share via


Copying DataSet Contents 

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.

  • Copy the schema, or relational structure, of the DataSet only, without copying any rows. Rows can be imported into an existing DataTable using ImportRow .

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.

Dim copyDataSet As DataSet = customerDataSet.Copy()
DataSet copyDataSet = customerDataSet.Copy();

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.
Dim changeDataSet As DataSet = customerDataSet.GetChanges()
' Copy only new rows.
Dim addedDataSetAs DataSet = _
    customerDataSet.GetChanges(DataRowState.Added)
// 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".

Dim germanyCustomers As DataSet = customerDataSet.Clone()

Dim copyRows() As DataRow = _
  customerDataSet.Tables("Customers").Select("CountryRegion = 'Germany'")

Dim customerTable As DataTable = germanyCustomers.Tables("Customers")
Dim copyRow As DataRow

For Each copyRow In copyRows
  customerTable.ImportRow(copyRow)
Next
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);

See Also

Reference

DataSet Class
DataTable Class

Other Resources

Using DataSets in ADO.NET