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:
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.)
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.
To add a query to a TableAdapter in the Dataset Designer
Open a dataset in the Dataset Designer. For more information, see How to: Open a Dataset in the Dataset Designer.
Right-click the desired TableAdapter, and select Add Query.
Drag a Query from the DataSet tab of the Toolbox onto a table on the designer.
The TableAdapter Query Configuration Wizard opens.
Complete the wizard; the query is added to the TableAdapter.
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
Select a TableAdapter in the component tray.
Click the TableAdapter's smart tag, and choose Add Query.
Complete the dialog box and the query is added to the TableAdapter. For more information, see Search Criteria Builder Dialog Box.