Delen via


How to: Directly Access the Database with a TableAdapter

In addition to the InsertCommand, UpdateCommand, and DeleteCommand, TableAdapters are created with methods that can be executed directly against the database. These methods (TableAdapter.Insert, TableAdapter.Update, and TableAdapter.Delete) can be called directly to manipulate data in the database.

If you do not want to create these direct methods, set the TableAdapter's GenerateDbDirectMethods property to false in the Properties window. Any queries added to a TableAdapter in addition to the TableAdapter's main query are standalone queries — they do not generate these DbDirect methods.

Sending Command 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 the Northwind database Region table as an example.

    Note

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

    Dim regionTableAdapter As New NorthwindDataSetTableAdapters.RegionTableAdapter
    
    regionTableAdapter.Insert(5, "NorthWestern")
    
    NorthwindDataSetTableAdapters.RegionTableAdapter regionTableAdapter = 
        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.

    Dim regionTableAdapter As New NorthwindDataSetTableAdapters.RegionTableAdapter
    
    regionTableAdapter.Update(1, "East", 1, "Eastern")
    
    NorthwindDataSetTableAdapters.RegionTableAdapter regionTableAdapter = 
        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. (This example uses the Northwind database's Region table.)

    Note

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

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

See Also

Concepts

Preparing Your Application to Receive Data

Fetching Data into Your Application

Binding Controls to Data in Visual Studio

Editing Data in Your Application

Validating Data

Saving Data

TableAdapter Overview

Other Resources

Overview of Data Applications in Visual Studio

Connecting to Data in Visual Studio

Commands and Parameters (ADO.NET)