HOW TO:執行可傳回巢狀集合的查詢 (EntityClient)

顯示如何使用 EntityCommand 物件針對概念模型執行命令,以及如何使用 EntityDataReader 擷取巢狀集合結果。

若要執行這個範例中的程式碼

  1. AdventureWorks 銷售模型 加入至專案中,並將專案設定為使用 Entity Framework 。 如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework)

  2. 在應用程式的字碼頁中加入下列 using 陳述式 (在 Visual Basic 中為 Imports):

    Imports System
    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
    
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Data.Common;
    using System.Data;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data.EntityClient;
    using System.Data.Metadata.Edm;
    

範例

巢狀集合」(Nested Collection) 是位於另一個集合內的集合。 下列程式碼會擷取 Contacts 的集合,以及與每一個 Contact 相關聯的 SalesOrderHeaders 巢狀集合。

Using conn As New EntityConnection("name=AdventureWorksEntities")
    conn.Open()
    ' Create an EntityCommand. 
    Using cmd As EntityCommand = conn.CreateCommand()
        ' Create a nested query. 
        Dim esqlQuery As String = "Select c.ContactID, c.SalesOrderHeaders" & _
            " From AdventureWorksEntities.Contacts as c"

        cmd.CommandText = esqlQuery
        ' Execute the command. 
        Using rdr As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            ' The result returned by this query contains 
            ' ContactID and a nested collection of SalesOrderHeader items. 
            ' associated with this Contact. 
            While rdr.Read()
                ' the first column contains Contact ID. 
                Console.WriteLine("Contact ID: {0}", rdr("ContactID"))

                ' The second column contains a collection of SalesOrderHeader 
                ' items associated with the Contact. 
                Dim nestedReader As DbDataReader = rdr.GetDataReader(1)
                While nestedReader.Read()
                    Console.WriteLine(" SalesOrderID: {0} ", nestedReader("SalesOrderID"))
                    Console.WriteLine(" OrderDate: {0} ", nestedReader("OrderDate"))
                End While
            End While
        End Using
    End Using
    conn.Close()
End Using
using (EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities"))
{
    conn.Open();
    // Create an EntityCommand.
    using (EntityCommand cmd = conn.CreateCommand())
    {
        // Create a nested query.
        string esqlQuery =
            @"Select c.ContactID, c.SalesOrderHeaders
        From AdventureWorksEntities.Contacts as c";

        cmd.CommandText = esqlQuery;
        // Execute the command.
        using (EntityDataReader rdr =
            cmd.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            // The result returned by this query contains 
            // ContactID and a nested collection of SalesOrderHeader items.
            // associated with this Contact.
            while (rdr.Read())
            {
                // the first column contains Contact ID.
                Console.WriteLine("Contact ID: {0}", rdr["ContactID"]);

                // The second column contains a collection of SalesOrderHeader 
                // items associated with the Contact.
                DbDataReader nestedReader = rdr.GetDataReader(1);
                while (nestedReader.Read())
                {
                    Console.WriteLine("   SalesOrderID: {0} ", nestedReader["SalesOrderID"]);
                    Console.WriteLine("   OrderDate: {0} ", nestedReader["OrderDate"]);
                }
            }
        }
    }
    conn.Close();
}

另請參閱

概念

Entity Framework 的 EntityClient 提供者