共用方式為


Directly access the database with a TableAdapter

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. 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. 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 the Region 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.

    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. The following procedure uses the Region 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.

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

See also