Aracılığıyla paylaş


How to: Set the Order When Performing a Hierarchical Update

When you save modified data in a dataset to a database (with hierarchical update enabled), you can control the order that the TableAdapterManager uses to send the individual Inserts, Updates, and Deletes that are required to save the data. The default order for performing a hierarchical update is Inserts, Updates, and then Deletes. The TableAdapterManager provides an UpdateOrder property that can be set to perform Updates first, then Inserts, and then Deletes.

It is important to understand that, by default, the TableAdapterManager first performs Inserts for all tables in the dataset, then Updates for all tables in the dataset, and then Deletes for all tables in the dataset.

Changing the update order to perform Updates first can be helpful when you are changing primary key values of existing records. Performing the Update first will modify the existing record as expected. Performing the Insert first will create a new record because the new primary key is not yet in the database.

Procedure

To set the order in which to perform a hierarchical update

  1. In Design view, open the form that contains the TableAdapterManager that is performing your update.

  2. Click the TableAdapterManager in the component tray.

  3. Set the UpdateOrder property in the Properties window to the order required for your application.

To programmatically set the order in which to perform a hierarchical update

  • The following code sets the TableAdapterManager to perform all Updates, then all Inserts, and then all Deletes. (Replace yourDataSetTableAdapters with a valid name from your project.)

    Me.TableAdapterManager.UpdateOrder = _
    yourDataSetTableAdapters.TableAdapterManager.UpdateOrderOption.UpdateInsertDelete
    
    this.tableAdapterManager.UpdateOrder = 
    yourDataSetTableAdapters.TableAdapterManager.UpdateOrderOption.UpdateInsertDelete;
    
  • The following code sets the TableAdapterManager to perform all Inserts, then all Updates, and then all Deletes. This is the default TableAdapterManager behavior.

    Me.TableAdapterManager.UpdateOrder = _
    yourDataSetTableAdapters.TableAdapterManager.UpdateOrderOption.InsertUpdateDelete
    
    this.tableAdapterManager.UpdateOrder = 
    yourDataSetTableAdapters.TableAdapterManager.UpdateOrderOption.InsertUpdateDelete;
    

See Also

Tasks

How to: Enable and Disable Hierarchical Update

Walkthrough: Saving Data from Related Data Tables (Hierarchical Update)

Concepts

Hierarchical Update Overview

Saving Data in Datasets

What's New for Data Application Development in Visual Studio 2012