Directly access the database with a TableAdapter in .NET Framework applications
Note
Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.
In addition to the InsertCommand
, UpdateCommand
, and DeleteCommand
, TableAdapters are created with methods that can be run directly against the database. You can call these methods (TableAdapter.Insert
, TableAdapter.Update
, and TableAdapter.Delete
) to manipulate data directly in the database.
If you don't want to create these direct methods, set the TableAdapter's GenerateDbDirectMethods
property to false
in the Properties window. If any queries are added to a TableAdapter in addition to the TableAdapter's main query, they are standalone queries that don't generate these DbDirect
methods.
Send commands directly to a database
Call the TableAdapter DbDirect
method that performs the task you are trying to accomplish.
To insert new records directly into a database
Call the TableAdapter's
Insert
method, passing in the values for each column as parameters. The following procedure uses theRegion
table in the Northwind database as an example.Note
If you do not have an instance available, instantiate the TableAdapter that you want to use.
To update records directly in a database
Call the TableAdapter's
Update
method, passing in the new and original values for each column as parameters.Note
If you do not have an instance available, instantiate the TableAdapter that you want to use.
To delete records directly from a database
Call the TableAdapter's
Delete
method, passing in the values for each column as parameters of theDelete
method. The following procedure uses theRegion
table in the Northwind database as an example.Note
If you do not have an instance available, instantiate the TableAdapter that you want to use.