Create parameterized TableAdapter queries
A parameterized query returns data that meets the conditions of a WHERE clause within the query. For example, you can parameterize a customer list 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.
You create parameterized TableAdapter queries in the Dataset Designer.You can also create them in a Windows application with the Parameterize Data Source command on the Data menu. The Parameterize Data Source command creates controls on your form where you can input the parameter values and run the query.
When constructing a parameterized query, use the parameter notation that's specific to the database you're coding against. For example, Access and OleDb data sources use the question mark '?' to denote parameters, so the WHERE clause would look like this:
The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or the edition you're using. To change your settings, go to the Tools menu and select Import and Export Settings. For more information, see Customizing Development Settings in Visual Studio.
Create a new TableAdapter, adding a WHERE clause with the desired parameters to the SQL statement. For more information, see Create and configure TableAdapters.
Add a query to an existing TableAdapter, adding a WHERE clause with the desired parameters to the SQL statement. For more information, See How to: Create TableAdapter Queries.
Select a control on your form that is already bound to a dataset. For more information, see Bind Windows Forms controls to data in Visual Studio.
On the Data menu, selectAdd Query.
Complete the Search Criteria Builder dialog box, adding a WHERE clause with the desired parameters to the SQL statement.
Open the form in the Windows Forms Designer.
On the Data menu, selectAdd QueryorData 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.
In the Select data source table area, select the tablethat you want to add parameterization to.
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.
In the Query Text box, type a query that takes parameters.
A control to input the parameter and a Load button are added to the form in a ToolStrip control.
TableAdapter parameters can be assigned null values when you want to query for records that have no current value. For example, consider the following query that has a
ShippedDate parameter in its
SELECT CustomerID, OrderDate, ShippedDate
WHERE (ShippedDate = @ShippedDate) OR
(ShippedDate IS NULL)
If this were a query on a TableAdapter, you could query for all orders that have not been shipped with the following code:
In the Dataset Designer, select the TableAdapter query that needs to accept null parameter values.
In the Properties window, selectParameters.Then press the ellipsis (…) button to open the Parameters Collection Editor.
Select the parameter that allows null values and set the AllowDbNull property to