Update two related tables in a dataset using TableAdapter

Source: Internet
Author: User

After you modify and validate the data in the dataset, you may need to send the updated data back to the database. To send the modified data to the database, you need to call the Update method. The update method of this adapter updates a single data table and executes the correct life (INSERT, Update, or DELETE) based on the RowState of each data row in the table.

    1. Call the adapter's Update method in the try/catch block.

    2. If an exception is caught, the data row that raised the error is found.

    3. Reconcile the problems in the data rows (either programmatically, if possible, or display the invalid rows to the user for modification), and then try the update again.

      Try    me.validate ()    Me.CustomersBindingSource.EndEdit ()    Me.CustomersTableAdapter.Update ( Me.NorthwindDataSet.Customers)    MsgBox ("Update successful") Catch ex as Exception    MsgBox ("Update failed") End Try

      Update two related tables in a dataset using TableAdapter

      When updating related tables in a dataset, it is important to update them in the correct order to reduce the likelihood of violating referential integrity constraints. The order in which the commands are executed will also follow the order of the indexes of the DataRowCollection in the dataset. To prevent data integrity errors from being raised, it is a best practice to update the database in the following order:

  1. Child table: Delete records.

  2. Parent table: Inserting, updating, and deleting records.

  3. Child tables: inserting and updating records.

  4. Update two related tables using TableAdapter
    1. Create three temporary data tables to hold different records.

    2. Call the Update method from the try/catch block for each child rowset. If an update error occurs, you should split the expense and resolve the errors.

    3. Commits the changes to the database.

    4. Dispose of temporary data tables to release resources.

      The following example shows how to update a data source with a dataset that contains related tables.

      Private Sub updatedb () Dim deletedchildrecords as Northwinddataset.ordersdatatable = _ CType (northwinddataset.or DERs. GetChanges (Data.DataRowState.Deleted), northwinddataset.ordersdatatable) Dim Newchildrecords as Northwinddataset.ordersdatatable = _ CType (NorthwindDataSet.Orders.GetChanges (Data.DataRowState.Added), northwindd ataset.ordersdatatable) Dim Modifiedchildrecords as Northwinddataset.ordersdatatable = _ CType (NorthwindDataSet. Orders.getchanges (Data.DataRowState.Modified), northwinddataset.ordersdatatable) Try If deletedchildrecords IsNo T Nothing Then orderstableadapter.update (deletedchildrecords) End If customerstableadapter.update (northwinddataset.customers) If newchildrecords IsNot Nothing Then Orderstableadapter.update (Newchildrec Ords) End If if modifiedchildrecords IsNot Nothing Then Orderstableadapter.update (modifiedchildre Cords) End If northwinddataseT.acceptchanges () Catch ex as Exception MessageBox.Show ("An error occurred during the update process") ' A    DD code to handle error here. Finally if deletedchildrecords IsNot Nothing Then Deletedchildrecords.dispose () End if If  Newchildrecords IsNot Nothing Then Newchildrecords.dispose () End if Modifiedchildrecords IsNot Nothing Then Modifiedchildrecords.dispose () End If End Tryend Sub




Update two related tables in a dataset using TableAdapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.