Export (0) Print
Expand All

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.

    NoteNote

    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");
    
    
    NorthwindDataSetRegionTableAdapter regionTableAdapter =
        new NorthwindDataSetRegionTableAdapter();
    
    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.

    NoteNote

    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");
    
    
    NorthwindDataSetRegionTableAdapter regionTableAdapter =
          new NorthwindDataSetRegionTableAdapter();
    
    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.)

    NoteNote

    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");
    
    
    NorthwindDataSetRegionTableAdapter regionTableAdapter =
          new NorthwindDataSetRegionTableAdapter();
    
    regionTableAdapter.Delete(5, "NorthWestern");
    
    

See Also

Community Additions

ADD
Show:
© 2014 Microsoft