共用方式為


DataServiceContext.ExecuteBatch 方法

以批次方式將一組查詢同步送出到資料服務。

不受到適用於 Silverlight 的 WCF Data Services 5.0 用戶端支援。

命名空間:  System.Data.Services.Client
組件:  Microsoft.Data.Services.Client (在 Microsoft.Data.Services.Client.dll 中)

語法

'宣告
Public Function ExecuteBatch ( _
    ParamArray queries As DataServiceRequest() _
) As DataServiceResponse
'用途
Dim instance As DataServiceContext
Dim queries As DataServiceRequest()
Dim returnValue As DataServiceResponse

returnValue = instance.ExecuteBatch(queries)
public DataServiceResponse ExecuteBatch(
    params DataServiceRequest[] queries
)
public:
DataServiceResponse^ ExecuteBatch(
    ... array<DataServiceRequest^>^ queries
)
member ExecuteBatch : 
        queries:DataServiceRequest[] -> DataServiceResponse 
public function ExecuteBatch(
    ... queries : DataServiceRequest[]
) : DataServiceResponse

參數

傳回值

型別:System.Data.Services.Client.DataServiceResponse
批次作業的回應。

備註

這些查詢會指定為 DataServiceRequest<TElement> 執行個體。 這個方法會傳回表示整個批次要求之回應的 DataServiceResponse。 個別查詢回應表示為個別 OperationResponse 物件,可透過列舉 DataServiceResponse 執行個體存取這些物件。

當此方法傳回時,已從網路資料流讀取批次要求的完整 HTTP 回應,但尚未處理回應。 除非回應中的指定之實體經過列舉,否則不會對該實體進行任何識別 (Identity) 解析或物件具體化。

範例

下列範例顯示如何呼叫 ExecuteBatch() 方法,執行 DataServiceRequest<TElement> 物件的陣列,其中包含從 Northwind 資料服務同時傳回 Customers 和 Products 物件的查詢。 列舉傳回之 DataServiceResponse 中的 QueryOperationResponse<T> 物件集合,也會列舉包含在每一個 QueryOperationResponse<T> 中的物件集合。 此範例使用「加入服務參考」工具所產生的 DataServiceContext,並根據完成 WCF Data Services 快速入門時所建立的 Northwind 資料服務。

Dim customerId = "ALFKI"

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Create the separate query URI's, one that returns 
' a single customer and another that returns all Products.
Dim customerUri = New Uri(svcUri.AbsoluteUri & _
    "/Customers('" & customerId & "')/?$expand=Orders")
Dim productsUri = New Uri(svcUri.AbsoluteUri & _
       "/Products")

' Create the query requests.
Dim customerQuery = New DataServiceRequest(Of Customer)(customerUri)
Dim productsQuery = New DataServiceRequest(Of Product)(productsUri)

' Add the query requests to a batch request array.
Dim batchRequests = _
    New DataServiceRequest() {customerQuery, productsQuery}

Dim batchResponse As DataServiceResponse

Try
    ' Execute the query batch and get the response.
    batchResponse = context.ExecuteBatch(batchRequests)

    If batchResponse.IsBatchResponse Then
        ' Parse the batchResponse.BatchHeaders.
    End If

    ' Enumerate over the results of the query.
    For Each response As QueryOperationResponse In batchResponse
        ' Handle an error response.
        If response.StatusCode > 299 OrElse response.StatusCode < 200 Then
            Console.WriteLine("An error occurred.")
            Console.WriteLine(response.Error.Message)
        Else
            ' Find the response for the Customers query.
            If response.Query.ElementType Is GetType(Customer) Then
                For Each customer As Customer In response
                    Console.WriteLine("Customer: {0}", customer.CompanyName)
                    For Each order As Order In customer.Orders
                        Console.WriteLine("Order ID: {0} - Freight: {1}", _
                                order.OrderID, order.Freight)
                    Next
                Next
                ' Find the response for the Products query.
            ElseIf response.Query.ElementType Is GetType(Product) Then
                For Each product As Product In response
                    Console.WriteLine("Product: {0}", product.ProductName)
                Next
            End If
        End If
    Next
    ' This type of error is raised when the data service returns with
    ' a response code < 200 or >299 in the top level element.
Catch ex As DataServiceRequestException
    ' Get the response from the exception.
    batchResponse = ex.Response

    If (batchResponse.IsBatchResponse) Then
        ' Parse the batchResponse.BatchHeaders.
    End If
    For Each response As QueryOperationResponse In batchResponse
        If response.Error IsNot Nothing Then
            Console.WriteLine("An error occurred.")
            Console.WriteLine(response.Error.Message)
        End If
    Next
End Try
string customerId = "ALFKI";

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Create the separate query URI's, one that returns 
// a single customer and another that returns all Products.
Uri customerUri = new Uri(svcUri.AbsoluteUri + 
    "/Customers('" + customerId + "')/?$expand=Orders");
Uri productsUri = new Uri(svcUri.AbsoluteUri +
   "/Products");

// Create the query requests.
DataServiceRequest<Customer> customerQuery = 
    new DataServiceRequest<Customer>(customerUri);
DataServiceRequest<Product> productsQuery =
                new DataServiceRequest<Product>(productsUri);

// Add the query requests to a batch request array.
DataServiceRequest[] batchRequests = 
    new DataServiceRequest[]{customerQuery, productsQuery};

DataServiceResponse batchResponse;

try
{
    // Execute the query batch and get the response.
    batchResponse = context.ExecuteBatch(batchRequests);

    if (batchResponse.IsBatchResponse)
    {
        // Parse the batchResponse.BatchHeaders.
    }  
    // Enumerate over the results of the query.
    foreach (QueryOperationResponse response in batchResponse)
    {
        // Handle an error response.
        if (response.StatusCode > 299 || response.StatusCode < 200)
        {
            Console.WriteLine("An error occurred.");
            Console.WriteLine(response.Error.Message);
        }
        else
        {
            // Find the response for the Customers query.
            if (response.Query.ElementType == typeof(Customer))
            {
                foreach (Customer customer in response)
                {
                    Console.WriteLine("Customer: {0}", customer.CompanyName);
                    foreach (Order order in customer.Orders)
                    {
                        Console.WriteLine("Order ID: {0} - Freight: {1}",
                            order.OrderID, order.Freight);
                    }
                }
            }
            // Find the response for the Products query.
            else if (response.Query.ElementType == typeof(Product))
            {
                foreach (Product product in response)
                {
                    Console.WriteLine("Product: {0}", product.ProductName);
                }
            }
        }
    }
}

// This type of error is raised when the data service returns with
// a response code < 200 or >299 in the top level element.
catch (DataServiceRequestException e)
{
    // Get the response from the exception.
    batchResponse = e.Response;

    if (batchResponse.IsBatchResponse)
    {
        // Parse the batchResponse.BatchHeaders.
    }

    foreach (QueryOperationResponse response in batchResponse)
    {
        if (response.Error != null)
        {
            Console.WriteLine("An error occurred.");
            Console.WriteLine(response.Error.Message);
        }
    }
}

請參閱

參考

DataServiceContext 類別

System.Data.Services.Client 命名空間

其他資源

HOW TO:在批次中執行查詢 (WCF Data Services)