Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Accessing Data
 How to: Add a Parameterized Query t...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Data Access in Client and Middle-Tier Programming
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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker