How to: Execute a Parameterized Entity SQL Query Using EntityCommand


This topic shows how to execute an Entity SQL query that has parameters by using an EntityCommand object.

To run the code in this example

  1. Add the AdventureWorks Sales Model to your project and configure your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard.

  2. In the code page for your application, add the following using statements (Imports in Visual Basic):

    [!CODE [DP EntityServices Concepts#Namespaces](../CodeSnippet/VS_Snippets_Data/dp entityservices concepts#namespaces)]

The following example shows how to construct a query string with two parameters. It then creates an EntityCommand, adds two parameters to the EntityParameter collection of that EntityCommand, and iterates through the collection of Contact items.

[!CODE [DP EntityServices Concepts#ParameterizedQueryWithEntityCommand](../CodeSnippet/VS_Snippets_Data/dp entityservices concepts#parameterizedquerywithentitycommand)]

How to: Execute a Parameterized Query
Entity SQL Language