本文整理汇总了C#中System.Data.Services.Client.DataServiceContext.ExecuteBatch方法的典型用法代码示例。如果您正苦于以下问题:C# DataServiceContext.ExecuteBatch方法的具体用法?C# DataServiceContext.ExecuteBatch怎么用?C# DataServiceContext.ExecuteBatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.Services.Client.DataServiceContext
的用法示例。
在下文中一共展示了DataServiceContext.ExecuteBatch方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NorthwindEntities
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);
}
}
}