How to: Use tableadapter to update data

Source: Internet
Author: User
How to: Use tableadapter to update data

After modifying and verifying 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 must callUpdateMethod. TheUpdateMethod: update a single data table and execute the correct command (insert, update, or delete) based on the rowstate of each data row in the table ).

Note:

An error may occur when you try to update the data source using the dataset content. Therefore,UpdateMethodCodePlaced inTry/CatchBlock.

Depending on your business needs, the exact process of updating the data source may be different, but your applicationProgramPerform the following steps:

    1. InTry/CatchBlock to callUpdateMethod.

    2. If an exception is caught, locate the data row that causes the error. For more information, see How to: locate an error row.

    3. Coordinate issues in data rows (programmatically, or display invalid rows to users for modification if possible), and then try to update (haserrors, geterrors) again ).

Save data to the database

CallUpdateMethod, pass the name of the data table, which contains the value to be written to the database.

Update a database using a dataset through tableadapter
  • InTry/CatchBlock containing the adapterUpdateMethod. The following example shows how to useNorthwinddatasetInMERsTable contentTry/CatchUpdate the block.

    Copy code in Visual Basic

    TryMe. Validate ()Me. Customersbindingsource. endedit ()Me. Customerstableadapter. Update (Me. Northwinddataset. Customers) msgbox ("Update successful")CatchExAsException msgbox ("Update failed")End Try

    C # copy code

    Try{This. Validate ();This. Customersbindingsource. endedit ();This. Customerstableadapter. Update (This. Northwinddataset. Customers); MessageBox. Show ("Update successful");}Catch(System. Exception ex) {MessageBox. Show ("Update failed");}

    J # copy code

    Try{This. Validate ();This. Customersbindingsource. endedit ();This. Northwinddatasetcustomerstableadapter. Update (This. Northwinddataset. get_customers (); MessageBox. Show ("Update successful");}Catch(System. Exception ex) {MessageBox. Show ("Update failed");}

Use tableadapter to update two related tables in the dataset

When updating related tables in a dataset, it is necessary to update them in the correct order to reduce the possibility of violating the integrity constraints of reference. The command execution sequence also follows the index sequence of datarowcollection in the dataset. To prevent data integrity errors, the best practice is to update the database in the following order:

    1. Sub-table: delete records.

    2. Parent table: insert, update, and delete records.

    3. Sub-table: insert and update records.

Note:

If you want to update two or more related tables, you should include all the update logic in one transaction. A transaction is a process. It first ensures that all relevant changes to the database can be successfully completed, and then submits the changes. For more information, see run transactions.

Use tableadapter to update two related tables
  1. Create three temporary data tables to save different records.

  2. SlaveTry/CatchBlock is called for each sub-row setUpdateMethod. If an update error occurs, branch it out and resolve it.

  3. Submit 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 containing the relevant table.

    Copy code in Visual Basic

     Private   Sub Updatedb () Dim Deletedchildrecords As Northwinddataset. ordersdatatable = _ ctype (northwinddataset. Orders. getchanges (data. datarowstate. Deleted), northwinddataset. ordersdatatable) Dim Newchildrecords As Northwinddataset. ordersdatatable = _ ctype (northwinddataset. Orders. getchanges (data. datarowstate. Added), northwinddataset. ordersdatatable) Dim Modifiedchildrecords As Northwinddataset. ordersdatatable = _ ctype (northwinddataset. Orders. getchanges (data. datarowstate. Modified), northwinddataset. ordersdatatable) Try      If Deletedchildrecords isnot Nothing   Then Orderstableadapter. Update (deletedchildrecords) End   If Customerstableadapter. Update (northwinddataset. MERs mers) If Newchildrecords isnot Nothing   Then Orderstableadapter. Update (newchildrecords) End   If      If Modifiedchildrecords isnot Nothing   Then Orderstableadapter. Update (modifiedchildrecords) End   If Northwinddataset. acceptchanges () Catch Ex As Exception MessageBox. Show ( "An error occurred during the update process" ) 'Add code to handle error here.      Finally      If Deletedchildrecords isnot Nothing   Then Deletedchildrecords. Dispose () End   If      If Newchildrecords isnot Nothing  Then Newchildrecords. Dispose () End   If      If Modifiedchildrecords isnot Nothing   Then Modifiedchildrecords. Dispose () End   If      End   Try      End   Sub     

    C # copy code

     Void Updatedb () {northwinddataset. ordersdatatable deletedchildrecords = (northwinddataset. ordersdatatable) northwinddataset. orders. getchanges (datarowstate. deleted); northwinddataset. ordersdatatable newchildrecords = (northwinddataset. ordersdatatable) northwinddataset. orders. getchanges (datarowstate. added); northwinddataset. ordersdatatable modifiedchildrecords = (northwinddataset. ordersdatatable) northwinddataset. orders. getchanges (datarowstate. modified ); Try { If (Deletedchildrecords! = Null ) {Orderstableadapter. Update (deletedchildrecords);} customerstableadapter. Update (northwinddataset. MERs mers ); If (Newchildrecords! = Null ) {Orderstableadapter. Update (newchildrecords );} If (Modifiedchildrecords! = Null ) {Orderstableadapter. Update (modifiedchildrecords);} northwinddataset. acceptchanges ();} Catch (Exception ex) {MessageBox. Show ( "An error occurred during the update process" ); // Add code to handle error here. } Finally { If (Deletedchildrecords! = Null ) {Deletedchildrecords. Dispose ();} If (Newchildrecords! = Null ) {Newchildrecords. Dispose ();} If (Modifiedchildrecords! = Null ) {Modifiedchildrecords. Dispose ();}}}

    J # copy code

     Void Updatedb () {northwinddataset. ordersdatatable deletedchildrecords = (northwinddataset. ordersdatatable) northwinddataset. get_orders (). getchanges (datarowstate. deleted); northwinddataset. ordersdatatable newchildrecords = (northwinddataset. ordersdatatable) northwinddataset. get_orders (). getchanges (datarowstate. added); northwinddataset. ordersdatatable modifiedchildrecords = (northwinddataset. ordersdatatable) northwinddataset. get_orders (). getchanges (datarowstate. modified );Try { If (Deletedchildrecords! = Null ) {Northwinddatasetorderstableadapter. Update (deletedchildrecords);} northwinddatasetcustomerstableadapter. Update (northwinddataset. get_customers ()); If (Newchildrecords! = Null ) {Northwinddatasetorderstableadapter. Update (newchildrecords );} If (Modifiedchildrecords! = Null ) {Northwinddatasetorderstableadapter. Update (modifiedchildrecords);} northwinddataset. acceptchanges ();} Catch (Exception ex) {MessageBox. Show ( "An error occurred during the update process" ); // Add code to handle error here. } Finally { If (Deletedchildrecords! = Null ) {Deletedchildrecords. Dispose ();} If (Newchildrecords! = Null ) {Newchildrecords. Dispose ();} If (Modifiedchildrecords! = Null ) {Modifiedchildrecords. Dispose ();}}}

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.