Creating a Parameterized Query

This lesson shows how to create a parameterized query in a LightSwitch application.

Create a Parameterized Query

A parameterized query makes use of one or more values that are determined at runtime to determine what data is returned. For example, a parameterized query might return a list of products that fall into a certain category. In this case, a parameter determines what category to use; it can be based on a value that is determined programmatically, or more commonly on a value that is entered by a user.

To create a parameterized query

  1. In Solution Explorer, select the Products node.

  2. On the Project menu, click Add Query.

    The Query Editor appears.

  3. In the Properties window, in the Name field, type ProductsByCategory.

  4. In the Query Designer, click Add Filter.

  5. In the first drop-down list, select Where.

  6. In the second drop-down list, select Category.CategoryID.

  7. In the third drop-down list, select = (equals).

  8. In the fourth drop-down list, select Parameter.

  9. In the final drop-down list, click Add New.

    A new Parameter named CategoryID, of type Integer is added to the Parameters section of the Query Editor.

Closer Look

This lesson showed how to create a parameterized query that returns a subset of products. At run time, a CategoryID box is provided so that a user can specify a numeric value for the CategoryID parameter. For example, if the user specifies 1 in the box, all products in the Beverages category will be returned. If the user specifies 2, Condiments will be returned.

The parameter value can be typed into a text box or selected from a list. It can also be provided by another entity on a screen. For example, when a product is selected on a screen, you could use its CategoryID as a parameter to return all other products in the same category.

Next Steps

In the next lesson, you will learn how to use the parameterized query that you just created on a screen.

Next lesson: Using a Parameterized Query on a Screen

See Also

Tasks

How to: Provide a Value to a Query Parameter

How to: Extend a Query by Using Code

Other Resources

Filtering Data with Queries

Queries: Retrieving Information from a Data Source