HOW TO:執行可傳回巢狀集合的查詢 (EntityClient)
本主題提供的範例將說明如何藉由使用 EntityCommand 對 Entity Data Model 執行命令,以及如何藉由使用 EntityDataReader 擷取巢狀集合結果。
若要執行這個範例中的程式碼
將 AdventureWorks Sales Model 加入至專案中,並將專案設定成使用 Entity Framework。若要這麼做,必須執行下列其中一項:
如果已經安裝 Entity Dada Model 工具,請完成 HOW TO:使用 Entity Data Model 精靈 (Entity Framework) 中的程序。
不然的話,請完成 HOW TO:手動設定 Entity Framework 專案和 HOW TO:以手動方式定義 Entity Data Model (Entity Framework) 中的程序。
在應用程式的字碼頁中加入下列 using 陳述式 (在 Visual Basic 中為 Imports):
Imports System Imports System.Collections.Generic Imports System.Collections Imports System.Data.Common Imports System.Data Imports System.Data.SqlClient Imports System.Data.EntityClient Imports System.Data.Metadata.Edm Imports System.IO ' Add AdventureWorksModel prepended with the root namespace for the project. 'Imports ProjectName.AdventureWorksModel
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 AdventureWorksModel; using System.Data.Metadata.Edm;
範例
「巢狀集合」(Nested Collection) 是某個位於另一個集合內的集合。下列程式碼會擷取 Contacts 的集合以及與每一個 Contact 有關聯的 SalesOrderHeaders 巢狀集合。
Using conn As EntityConnection = New EntityConnection("name=AdventureWorksEntities")
conn.Open()
Try
' Create an EntityCommand.
Using cmd As EntityCommand = conn.CreateCommand()
' Create a nested query.
Dim esqlQuery As String = "Select c.ContactID, c.SalesOrderHeader " & _
"From AdventureWorksEntities.Contact 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.
Do While rdr.Read
' the first column contains Contact ID.
Console.WriteLine("Contact ID: {0}", rdr.Item("ContactID"))
' The second column contains a collection of SalesOrderHeader
' items associated with the Contact.
Dim nestedReader As DbDataReader = rdr.GetDataReader(1)
Do While nestedReader.Read
Console.WriteLine(" SalesOrderID: {0} ", nestedReader.Item("SalesOrderID"))
Console.WriteLine(" OrderDate: {0} ", nestedReader.Item("OrderDate"))
Loop
Loop
End Using
End Using
Catch ex As EntityException
Console.WriteLine(ex.ToString())
End Try
conn.Close()
End Using
using (EntityConnection conn =
new EntityConnection("name=AdventureWorksEntities"))
{
conn.Open();
try
{
// Create an EntityCommand.
using (EntityCommand cmd = conn.CreateCommand())
{
// Create a nested query.
string esqlQuery =
@"Select c.ContactID, c.SalesOrderHeader
From AdventureWorksEntities.Contact 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"]);
}
}
}
}
}
catch (EntityException ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
}