DataServiceContext.ExecuteBatch(DataServiceRequest[]) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将一组查询以批处理方式提交到数据服务。
public:
System::Data::Services::Client::DataServiceResponse ^ ExecuteBatch(... cli::array <System::Data::Services::Client::DataServiceRequest ^> ^ queries);
public System.Data.Services.Client.DataServiceResponse ExecuteBatch (params System.Data.Services.Client.DataServiceRequest[] queries);
member this.ExecuteBatch : System.Data.Services.Client.DataServiceRequest[] -> System.Data.Services.Client.DataServiceResponse
Public Function ExecuteBatch (ParamArray queries As DataServiceRequest()) As DataServiceResponse
参数
- queries
- DataServiceRequest[]
组成查询的 DataServiceRequest 对象的数组。
返回
批处理操作的响应。
示例
下面的示例演示如何调用 ExecuteBatch 方法来执行一个 DataServiceRequest<TElement> 对象数组,该数组包含从 Northwind 数据服务同时返回 Customers
和 Products
对象的查询。 此示例将枚举返回的 QueryOperationResponse<T> 中的 DataServiceResponse 对象的集合,并且还会枚举每个 QueryOperationResponse<T> 中包含的对象的集合。 此示例使用DataServiceContext基于 Northwind 数据服务的“添加服务引用”工具生成的 ,该服务是在完成WCF Data Services 时创建的。
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);
}
}
}
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
注解
将查询指定为 DataServiceRequest<TElement> 实例。 返回表示整个批处理请求响应的 DataServiceResponse。 单个查询响应表示为单个 OperationResponse 对象,可通过枚举 DataServiceResponse 实例进行访问。
此方法返回时,已从网络流读取批处理请求的整个 HTTP 响应,但尚未对这些响应进行处理。 在枚举响应中的指定实体之前,未对该实体进行标识解析或对象具体化操作。