Freigeben über


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.

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

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 Customizing Development Settings in Visual Studio.

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.

  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

Creating and Editing Typed Datasets

Data Sources Overview

Validating Data