How to: Execute TableAdapter Queries
The document is archived and information here might be outdated

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.


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.


  • 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.


  • Create a TableAdapter instance programmatically:

    NorthwindDataSetTableAdapters.CustomersTableAdapter customersTableAdapter1;
    customersTableAdapter1 = new NorthwindDataSetTableAdapters.CustomersTableAdapter();
    NorthwindDataSetCustomersTableAdapter customersTableAdapter1;
    customersTableAdapter1 = new NorthwindDataSetCustomersTableAdapter();

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:


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:

    NorthwindDataSet.CustomersDataTable newCustomersTable;
    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.

    NorthwindDataSetTableAdapters.QueriesTableAdapter scalarQueriesTableAdapter;
    scalarQueriesTableAdapter = new NorthwindDataSetTableAdapters.QueriesTableAdapter();
    int returnValue;
    returnValue = (int)scalarQueriesTableAdapter.CustomerCount();
    NorthwindDataSetQueriesTableAdapter scalarQueriesTableAdapter;
    scalarQueriesTableAdapter = new NorthwindDataSetQueriesTableAdapter();
    int returnValue;
    returnValue = scalarQueriesTableAdapter.CustomerCount().get_Value();

See Also

© 2016 Microsoft