How to: Create TableAdapter Queries

TableAdapter queries are SQL statements or stored procedures that your application can execute against a database.

Add as many queries to a TableAdapter as your application requires. TableAdapter queries appear as methods on a TableAdapter. When you create a query called FillByCity that takes a parameter representing the city value, the query is added to the TableAdapter. It is added as a typed method that takes the correct type of parameter as an argument — in this case a string representing the city value. You call the TableAdapter query just like any method on any object. For example, the following code executes the FillByCity query and fills the Customers table with all customers with a city value of Seattle:

Dim cityValue As String = "Seattle"
CustomersTableAdapter.FillByCity(NorthwindDataSet.Customers, cityValue)
string cityValue = "Seattle";
customersTableAdapter.FillByCity(northwindDataSet.Customers, cityValue);

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. (Right-click any TableAdapter and choose Add Query.)

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

Create a query in the Dataset Designer

To add a query to a TableAdapter in the Dataset Designer

  1. Open a dataset in the Dataset Designer. For more information, see How to: Open a Dataset in the Dataset Designer.

  2. Right-click the desired TableAdapter, and select Add Query.

    -or-

  3. Drag a Query from the DataSet tab of the Toolbox onto a table on the designer.

    The TableAdapter Query Configuration Wizard opens.

  4. Complete the wizard; the query is added to the TableAdapter.

Create a Query Directly on a Form in Your Windows Application

If you have an instance of a TableAdapter on your form you can add a query using the Search Criteria Builder Dialog Box, which adds a ToolStrip control to the form that accepts any input parameters required by the query, as well as a button to run the query.

To add a query to a TableAdapter using the Search Criteria dialog box

  1. Select a TableAdapter in the component tray.

  2. Click the TableAdapter's smart tag, and choose Add Query.

  3. Complete the dialog box and the query is added to the TableAdapter. For more information, see Search Criteria Builder Dialog Box.

See Also

Tasks

How to: Edit TableAdapter Queries

How to: Connect to Data in a Database

How to: Navigate Data with the Windows Forms BindingNavigator Control

Walkthrough: Displaying Data on a Windows Form

Walkthrough: Creating a TableAdapter with Multiple Queries

Concepts

TableAdapter Overview

Creating and Editing Typed Datasets

Data Sources Overview

Validating Data