Share via


Directly access the database with a TableAdapter

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

In addition to the InsertCommand, UpdateCommand, and DeleteCommand, TableAdapters are created with methods that can be run directly against the database. These methods (TableAdapter.Insert, TableAdapter.Update, and TableAdapter.Delete) can be called 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.

Sendcommandsdirectly 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 the Region table in the Northwind databaseas an example.

    Note

    If you do not have an instance available, instantiate the TableAdapter that you want to use.

    NorthwindDataSetTableAdapters.RegionTableAdapter regionTableAdapter = 
        new NorthwindDataSetTableAdapters.RegionTableAdapter();
    
    regionTableAdapter.Insert(5, "NorthWestern");
    
    Dim regionTableAdapter As New NorthwindDataSetTableAdapters.RegionTableAdapter
    
    regionTableAdapter.Insert(5, "NorthWestern")
    

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.

    NorthwindDataSetTableAdapters.RegionTableAdapter regionTableAdapter = 
        new NorthwindDataSetTableAdapters.RegionTableAdapter();
    
    regionTableAdapter.Update(1, "East", 1, "Eastern");
    
    Dim regionTableAdapter As New NorthwindDataSetTableAdapters.RegionTableAdapter
    
    regionTableAdapter.Update(1, "East", 1, "Eastern")
    

To delete records directly from a database

  • Call the TableAdapter's Delete method, passing in the values for each column as parameters of the Delete method. The following procedure uses the Region table in the Northwind databaseas an example.

    Note

    If you do not have an instance available, instantiate the TableAdapter that you want to use.

    NorthwindDataSetTableAdapters.RegionTableAdapter regionTableAdapter = 
        new NorthwindDataSetTableAdapters.RegionTableAdapter();
    
    regionTableAdapter.Delete(5, "NorthWestern");
    
    Dim regionTableAdapter As New NorthwindDataSetTableAdapters.RegionTableAdapter
    
    regionTableAdapter.Delete(5, "NorthWestern")
    

See Also

Fill datasets by using TableAdapters