DataServiceContext.ExecuteBatch Method (DataServiceRequest())


Submits a group of queries as a batch to the data service.

Namespace:   System.Data.Services.Client
Assembly:  System.Data.Services.Client (in System.Data.Services.Client.dll)

Public Function ExecuteBatch (
	ParamArray queries As DataServiceRequest()
) As DataServiceResponse


Type: System.Data.Services.Client.DataServiceRequest()

Array of T:System.Data.Services.Client.DataServiceRequest[] objects that make up the queries.

Return Value

Type: System.Data.Services.Client.DataServiceResponse

The response to the batch operation.

The queries are specified as DataServiceRequest(Of TElement) instances. Returns a DataServiceResponse that represents the response of the batch request as a whole. Individual query responses are represented as individual OperationResponse objects that can be accessed by enumerating the DataServiceResponse instance.

When this method returns the whole HTTP response for the batch request has been read from the network stream, but the responses have not been processed. No identity resolution or object materialization occurs for a specified entity in the response until it is enumerated.

The following example shows how to call the M:Microsoft.SqlServer.ReportingServices.ReportingService.ExecuteBatch method to execute an array of DataServiceRequest(Of TElement) objects that contains queries that return both Customers and Products objects from the Northwind data service. The collection of QueryOperationResponse(Of T) objects in the returned DataServiceResponse is enumerated, and the collection of objects that is contained in each QueryOperationResponse(Of T) is also enumerated. This example uses the DataServiceContext generated by the Add Service Reference tool based on the Northwind data service, which is created when you complete the WCF Data Services .

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 & _

' 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

    ' 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.")
            ' 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)
                ' 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)
            End If
        End If
    ' 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.")
        End If
End Try

.NET Framework
Available since 3.5
Return to top