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)

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

NoteNote

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.

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.

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft