Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
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: Add a Parameterized Query to a Form in a Windows 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.

NoteNote:

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

    NoteNote:

    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.

    TipTip:

    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.

Community Additions

ADD
Show:
© 2015 Microsoft