|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.
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
Open the form in the Windows Forms Designer.
Click Add Query on the Data menu or Data Smart Tags.
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.
Select the desired table to add parameterization in the Select data source table area. For more information, see Search Criteria Builder Dialog Box.
Type a name in the New query name box if you are creating a new query.
Select a query in the Existing query name box.
Type a query that takes parameters in the Query Text box.
Use the Query Builder to assist in creating a query. For more information, see Query and View Designer Tools.
A control to input the parameter and a Load button are added to the form in a ToolStrip control.