複製 DataSet 內容
您可以建立 DataSet 的複本,這樣就可以在不影響原始資料的情況下使用資料,也可以從 DataSet 使用資料的子集。複製 DataSet 時,您可以:
建立與 DataSet 完全相同的複本,包括結構描述、資料、資料列狀態資訊和資料列版本。
建立一個 DataSet,其中包含現有 DataSet 的結構描述,但只有修改過的資料列。您可以傳回所有修改過的資料列,或指定特定的 DataRowState。如需資料列狀態的詳細資訊,請參閱資料列狀態和資料列版本。
只複製 DataSet 的結構描述或關聯式結構,而不複製任何資料列。使用 ImportRow,可以將資料列匯入現有的 DataTable。
若要建立包含結構描述和資料的 DataSet 完整複本,請使用 DataSet 的 Copy 方法。下列程式碼範例顯示如何建立 DataSet 的完整複本。
Dim copyDataSet As DataSet = customerDataSet.Copy()
DataSet copyDataSet = customerDataSet.Copy();
若要建立 DataSet 的複本,其中包含結構描述和只表示 Added、Modified 或 Deleted 資料列的資料,請使用 DataSet 的 GetChanges 方法。呼叫 GetChanges 時,您也可以使用 GetChanges 傳遞 DataRowState 的值,而只傳回具有指定資料列狀態的資料列。下列程式碼範例顯示如何在呼叫 GetChanges 時傳遞 DataRowState。
' 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);
若要建立只包含結構描述的 DataSet 複本,請使用 DataSet 的 Clone 方法。您也可以使用 DataTable 的 ImportRow 方法,將現有資料列加入複製的 DataSet。ImportRow 會將資料、資料列狀態和資料列版本資訊加入至指定資料表。資料行值只會被加入資料行名稱相符且資料型別相容之處。
下列程式碼範例建立 DataSet 的複製品,然後將原始 DataSet 的資料列加入 DataSet 複製品中的 Customers 資料表,該表中 CountryRegion 資料行的值為 "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);