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:
InTry/CatchBlock to callUpdateMethod.
If an exception is caught, locate the data row that causes the error. For more information, see How to: locate an error row.
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:
Sub-table: delete records.
Parent table: insert, update, and delete records.
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
-
Create three temporary data tables to save different records.
-
SlaveTry/CatchBlock is called for each sub-row setUpdateMethod. If an update error occurs, branch it out and resolve it.
-
Submit the changes to the database.
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 ();}}}