Partilhar via


How to: Execute TableAdapter Queries

TableAdapter queries are SQL statements or stored procedures that your application can execute against a database and are exposed as typed methods on the TableAdapter. You can execute TableAdapter queries by calling the associated methods, just as with any method on any object.

TableAdapter queries can fill a data table (Fill and FillBy queries) or return new data tables populated with the data returned by the query (GetData and GetDataBy queries).

You can add queries to existing TableAdapters by running the TableAdapter Query Configuration Wizard.

Create an Instance of the TableAdapter

You must create an instance of the TableAdapter before being able to call any of its methods.

Note

Even though TableAdapters are created and edited using the Dataset Designer, TableAdapters are not actually nested classes within a dataset. TableAdapters are located within a namespace that is identified based on the name of the dataset associated with the TableAdapter. The naming convention is: DataSetName + "TableAdapters". For example, all TableAdapters associated with the NorthwindDataSet will be located in the NorthwindDataSetTableAdapters namespace. If there is a CustomersTableAdapter, then its fully qualified name would be NorthwindDataSetTableAdapters.CustomersTableAdapter.

To create an instance of the TableAdapter

  • Drag items from the Data Sources window onto a form in your Windows application to automatically create an instance of a TableAdapter on the form. Inspect the TableAdapter in the component tray (the small area below the bottom border of the form) for the name of the instance.

    -or-

  • After creating a TableAdapter, build the project. The TableAdapter then appears in the Toolbox. Drag the TableAdapter from the Toolbox onto a form to create an instance. Inspect the TableAdapter in the component tray for the name of the instance.

    -or-

  • Create a TableAdapter instance programmatically:

    Dim CustomersTableAdapter1 As NorthwindDataSetTableAdapters.CustomersTableAdapter
    CustomersTableAdapter1 = New NorthwindDataSetTableAdapters.CustomersTableAdapter()
    
    NorthwindDataSetTableAdapters.CustomersTableAdapter customersTableAdapter1;
    customersTableAdapter1 = new NorthwindDataSetTableAdapters.CustomersTableAdapter();
    

Execute TableAdapter Queries That Fill Existing Data Tables (Fill() Methods)

To execute a TableAdapter query that fills an existing data table

  • Call the TableAdapter's Fill or FillBy query and pass in the data table to fill. For example, the following code executes the Fill query and fills the Customers table:

    CustomersTableAdapter1.Fill(NorthwindDataSet1.Customers)
    
    customersTableAdapter1.Fill(northwindDataSet1.Customers);
    

Execute TableAdapter Queries That Return New Data Tables (GetData() Methods)

To execute a TableAdapter query that returns a new data table

  • Call the TableAdapter's GetData or GetDataBy query to return a typed data table filled with the results of the query. For example, the following code executes the GetData query and returns a Customers table:

    Dim newCustomersTable As NorthwindDataSet.CustomersDataTable
    newCustomersTable = CustomersTableAdapter1.GetData()
    
    NorthwindDataSet.CustomersDataTable newCustomersTable;
    newCustomersTable = customersTableAdapter1.GetData();
    

Execute TableAdapter Queries That Return Single (Scalar) Values

You can drag a query from the Toolbox directly onto the Dataset Designer to create a stand-alone query (a query with no data table).

To execute a TableAdapter query that returns a single (scalar) value

  • Create an instance of the TableAdapter, declare a variable to hold the return value, and assign it the result of the query. The following example assumes that there is a query named CustomerCount on the QueriesTableAdapter.

    Dim scalarQueriesTableAdapter As NorthwindDataSetTableAdapters.QueriesTableAdapter
    scalarQueriesTableAdapter = New NorthwindDataSetTableAdapters.QueriesTableAdapter()
    
    Dim returnValue As Integer
    returnValue = CType(scalarQueriesTableAdapter.CustomerCount(), Integer)
    
    NorthwindDataSetTableAdapters.QueriesTableAdapter scalarQueriesTableAdapter;
    scalarQueriesTableAdapter = new NorthwindDataSetTableAdapters.QueriesTableAdapter();
    
    int returnValue;
    returnValue = (int)scalarQueriesTableAdapter.CustomerCount();
    

See Also

Tasks

How to: Create TableAdapters

How to: Create TableAdapter Queries

How to: Edit TableAdapter Queries

How to: Directly Access the Database with a TableAdapter

Walkthrough: Saving Data with the TableAdapter DBDirect Methods

How to: Navigate Data with the Windows Forms BindingNavigator Control

Walkthrough: Displaying Data on a Windows Form

Concepts

TableAdapter Overview

Fetching Data into Your Application

Binding Windows Forms Controls to Data in Visual Studio

Creating and Editing Typed Datasets

Data Sources Overview

Validating Data