
Update Existing Records Using TableAdapters
TableAdapters provide different ways to update records in a database depending on the requirements of your application.
If your application uses datasets to store data, then you can simply update the records in the desired DataTable and then call the TableAdapter.Update method and pass in either the DataSet, DataTable, DataRow, or array of DataRows. The table above describes the different Update methods.
To update records in a database with the TableAdapter.Update method that takes DataSet, DataTable, DataRow, or DataRows()
Edit records in the desired DataTable by directly editing the DataRow in the DataTable. For more information, see How to: Edit Rows in a DataTable.
After the rows are edited in the DataTable, call the TableAdapter.Update method. You can control the amount of data to update by passing in either an entire DataSet, a DataTable, an array of DataRows, or a single DataRow.
The following code shows how to edit a record in a DataTable and then call the TableAdapter.Update method to save the changes to the database. (This example uses the Northwind database Region table.)
' Locate the row you want to update.
Dim regionRow As NorthwindDataSet.RegionRow
regionRow = NorthwindDataSet._Region.FindByRegionID(1)
' Assign the new value to the desired column.
regionRow.RegionDescription = "East"
' Save the updated row to the database
Me.RegionTableAdapter.Update(Me.NorthwindDataSet._Region)
// Locate the row you want to update.
NorthwindDataSet.RegionRow regionRow;
regionRow = northwindDataSet.Region.FindByRegionID(1);
// Assign the new value to the desired column.
regionRow.RegionDescription = "East";
// Save the updated row to the database.
this.regionTableAdapter.Update(this.northwindDataSet.Region);
If your application uses objects to store the data in your application, you can use the TableAdapter's DBDirect methods to send data from your objects directly to the database. These methods allow you to pass individual values for each column as method parameters. Calling this method updates an existing record in the database with the column values passed into the method.
The following procedure uses the Northwind Region table as an example.
To update records in a database using the TableAdapter.Update method that takes column values