|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
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:
string cityValue = "Seattle"; customersTableAdapter.FillByCity(northwindDataSet.Customers, cityValue);
String cityValue = "Seattle"; northwindDataSetCustomersTableAdapter.FillByCity(northwindDataSet.get_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. (Right-click any TableAdapter and choose Add Query.)
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see.
Create a query in the Dataset Designer
To add a query to a TableAdapter in the Dataset Designer
Open a dataset in the Dataset Designer. For more information, see.
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.
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, which adds a 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.