共用方式為


作法:使用 EntityCommand 執行參數化 Entity SQL 查詢

本主題示範如何使用 EntityCommand 物件來執行具有參數的 Entity SQL 查詢。

執行此範例中的程式碼

  1. AdventureWorks Sales Model 加入至專案中,並將專案設定成使用 Entity Framework。 如需詳細資訊,請參閱如何:使用實體資料模型精靈

  2. 在應用程式的代碼頁中,新增下列 using 指示詞 (Imports 在 Visual Basic 中):

    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

另請參閱