如何:使用 EntityCommand 执行参数化实体 SQL 查询

本主题介绍如何使用 EntityCommand 对象执行具有参数的 实体 SQL 查询。

运行本示例中的代码

  1. AdventureWorks 销售模型添加到项目并将项目配置为使用实体框架。 有关详细信息,请参阅如何:使用实体数据模型向导

  2. 在应用程序的代码页中,添加以下 using 指令(在 Visual Basic 中为 Imports):

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.EntityClient;
    using System.Data.Metadata.Edm;
    
    Imports System.Collections.Generic
    Imports System.Collections
    Imports System.Data.Common
    Imports System.Data
    Imports System.IO
    Imports System.Data.SqlClient
    Imports System.Data.EntityClient
    Imports System.Data.Metadata.Edm
    
    

示例

以下示例显示如何使用两个参数构造一个查询字符串。 然后,它创建 EntityCommand,将两个参数添加到该 EntityParameterEntityCommand 集合,并循环访问 Contact 项的集合。

using (EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities"))
{
    conn.Open();
    // Create a query that takes two parameters.
    string esqlQuery =
        @"SELECT VALUE Contact FROM AdventureWorksEntities.Contacts
                    AS Contact WHERE Contact.LastName = @ln AND
                    Contact.FirstName = @fn";

    using (EntityCommand cmd = new EntityCommand(esqlQuery, conn))
    {
        // Create two parameters and add them to
        // the EntityCommand's Parameters collection
        EntityParameter param1 = new EntityParameter();
        param1.ParameterName = "ln";
        param1.Value = "Adams";
        EntityParameter param2 = new EntityParameter();
        param2.ParameterName = "fn";
        param2.Value = "Frances";

        cmd.Parameters.Add(param1);
        cmd.Parameters.Add(param2);

        using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            // Iterate through the collection of Contact items.
            while (rdr.Read())
            {
                Console.WriteLine(rdr["FirstName"]);
                Console.WriteLine(rdr["LastName"]);
            }
        }
    }
    conn.Close();
}
Using conn As New EntityConnection("name=AdventureWorksEntities")
    conn.Open()
    ' Create a query that takes two parameters. 
    Dim esqlQuery As String = "SELECT VALUE Contact FROM AdventureWorksEntities.Contacts " & _
        " AS Contact WHERE Contact.LastName = @ln AND Contact.FirstName = @fn"

    Using cmd As New EntityCommand(esqlQuery, conn)
        ' Create two parameters and add them to 
        ' the EntityCommand's Parameters collection 
        Dim param1 As New EntityParameter()
        param1.ParameterName = "ln"
        param1.Value = "Adams"
        Dim param2 As New EntityParameter()
        param2.ParameterName = "fn"
        param2.Value = "Frances"

        cmd.Parameters.Add(param1)
        cmd.Parameters.Add(param2)

        Using rdr As DbDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            ' Iterate through the collection of Contact items. 
            While rdr.Read()
                Console.WriteLine(rdr("FirstName"))
                Console.WriteLine(rdr("LastName"))
            End While
        End Using
    End Using
    conn.Close()
End Using

请参阅