Share via


How to: Add a Parameterized Query to a Windows Forms Application

Adding search functionality to a form in a Windows application can be accomplished by running a parameterized query. A parameterized query returns data that meets the conditions of a WHERE clause. You add parameterization to a query by completing the Search Criteria Builder Dialog Box. For example, you can parameterize a query to display only customers in a certain city by adding WHERE City = @City to the end of the SQL statement that returns a list of customers. For more information, see Parameter Queries.

Completing the Search Criteria Dialog Box does the following:

  • The query is added to the associated TableAdapter.

  • Controls are added to the form for users to provide any required parameter values, and a button is added to execute the query.

  • Code is added to the button's click event that sets parameter values and executes the 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 Visual Studio Settings.

To add a query to an existing data-bound form

  1. Open the form in the Windows Forms Designer.

  2. Click Add Query on the Data menu or Data Smart Tags.

    Note

    If Add Query is not available on the Data menu, select a control on the form that displays the data source you want to add the parameterization to. For example, if the form displays data in a DataGridView control, select it. If the form displays data in individual controls, select any data-bound control.

  3. Select the desired table to add parameterization in the Select data source table area. For more information, see Search Criteria Builder Dialog Box.

  4. Type a name in the New query name box if you are creating a new query.

    -or-

    Select a query in the Existing query name box.

  5. Type a query that takes parameters in the Query Text box.

    Tip

    Use the Query Builder to assist in creating a query. For more information, see Query and View Designer Tools.

  6. Click OK.

    A control to input the parameter and a Load button are added to the form in a ToolStrip control.

See Also

Tasks

Walkthrough: Creating a Windows Form to Search Data

Walkthrough: Displaying Data on a Windows Form

How to: Connect to Data in a Database

How to: Navigate Data with the Windows Forms BindingNavigator Control

How to: Sort and Filter ADO.NET Data with the Windows Forms BindingSource Component

How to: Create a Lookup Table with the Windows Forms BindingSource Component

Reference

Data Sources Window

Concepts

TableAdapter Overview

Dataset Designer

Data Sources Overview

Validating Data