DataServiceContext.Execute 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向数据服务发送请求以执行特定 URI。
重载
| 名称 | 说明 |
|---|---|
| Execute<T>(DataServiceQueryContinuation<T>) |
向数据服务发送请求,以检索分页查询结果中的下一页数据。 |
| Execute<TElement>(Uri) |
向数据服务发送请求以执行特定 URI。 |
Execute<T>(DataServiceQueryContinuation<T>)
向数据服务发送请求,以检索分页查询结果中的下一页数据。
public:
generic <typename T>
System::Data::Services::Client::QueryOperationResponse<T> ^ Execute(System::Data::Services::Client::DataServiceQueryContinuation<T> ^ continuation);
public System.Data.Services.Client.QueryOperationResponse<T> Execute<T>(System.Data.Services.Client.DataServiceQueryContinuation<T> continuation);
member this.Execute : System.Data.Services.Client.DataServiceQueryContinuation<'T> -> System.Data.Services.Client.QueryOperationResponse<'T>
Public Function Execute(Of T) (continuation As DataServiceQueryContinuation(Of T)) As QueryOperationResponse(Of T)
类型参数
- T
查询返回的类型。
参数
- continuation
- DataServiceQueryContinuation<T>
一个对象,表示要从数据服务返回的下一 DataServiceQueryContinuation<T> 页数据。
返回
包含查询结果中下一页数据的响应。
例外
在执行请求期间或将响应消息的内容转换为对象时引发错误时。
注解
提供的 DataServiceQueryContinuation<T> 对象包含执行时返回查询结果中下一页数据的 URI。
适用于
Execute<TElement>(Uri)
向数据服务发送请求以执行特定 URI。
public:
generic <typename TElement>
System::Collections::Generic::IEnumerable<TElement> ^ Execute(Uri ^ requestUri);
public System.Collections.Generic.IEnumerable<TElement> Execute<TElement>(Uri requestUri);
member this.Execute : Uri -> seq<'Element>
Public Function Execute(Of TElement) (requestUri As Uri) As IEnumerable(Of TElement)
类型参数
- TElement
查询返回的类型。
参数
- requestUri
- Uri
将向其发送查询请求的 URI。 URI 可以是任何有效的数据服务 URI。 可以包含 $ 查询参数。
返回
查询操作的结果。
例外
当未从请求接收响应时 requestUri。
何时 requestUri 为 null。
当不是数据服务的有效 URI 时 requestUri 。
在执行请求期间或将响应消息的内容转换为对象时引发错误时。
数据服务返回 HTTP 404:找不到资源错误。
示例
此示例使用循环 do…while 从数据服务分页结果加载 Customers 实体。
Execute通过使用下一个链接 URI 来接收下一页的数据,调用该方法。
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
DataServiceQueryContinuation<Customer> token = null;
int pageCount = 0;
try
{
// Execute the query for all customers and get the response object.
QueryOperationResponse<Customer> response =
context.Customers.Execute() as QueryOperationResponse<Customer>;
// With a paged response from the service, use a do...while loop
// to enumerate the results before getting the next link.
do
{
// Write the page number.
Console.WriteLine("Page {0}:", pageCount++);
// If nextLink is not null, then there is a new page to load.
if (token != null)
{
// Load the new page from the next link URI.
response = context.Execute<Customer>(token)
as QueryOperationResponse<Customer>;
}
// Enumerate the customers in the response.
foreach (Customer customer in response)
{
Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
}
}
// Get the next link, and continue while there is a next link.
while ((token = response.GetContinuation()) != null);
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Dim token As DataServiceQueryContinuation(Of Customer) = Nothing
Dim pageCount = 0
Try
' Execute the query for all customers and get the response object.
Dim response As QueryOperationResponse(Of Customer) =
CType(context.Customers.Execute(), QueryOperationResponse(Of Customer))
' With a paged response from the service, use a do...while loop
' to enumerate the results before getting the next link.
Do
' Write the page number.
Console.WriteLine("Page {0}:", pageCount + 1)
' If nextLink is not null, then there is a new page to load.
If token IsNot Nothing Then
' Load the new page from the next link URI.
response = CType(context.Execute(Of Customer)(token),
QueryOperationResponse(Of Customer))
End If
' Enumerate the customers in the response.
For Each customer As Customer In response
Console.WriteLine(vbTab & "Customer Name: {0}", customer.CompanyName)
Next
' Get the next link, and continue while there is a next link.
token = response.GetContinuation()
Loop While token IsNot Nothing
Catch ex As DataServiceQueryException
Throw New ApplicationException(
"An error occurred during query execution.", ex)
End Try
注解
该方法 Execute 用于按 URI 查询数据服务;该方法会导致向数据服务发出 HTTP GET 请求。 指定的请求 URI 可以是绝对 URI 或相对 URI。
如果为绝对值 requestUri ,此方法将验证 URI 是否指向构造 DataServiceContext时指定的相同数据服务。
requestUri如果为相对值,此方法将去除任何前导斜杠,并在构造requestUri时追加DataServiceContext到提供的内容。 如果不存在斜杠,则会在传递给构造函数的 DataServiceContext URI 之后追加斜杠。
此方法返回时,请求的所有 HTTP 响应已从网络流中读取,但不会处理响应;没有标识解析或对象具体化。 在枚举指定实体之前,不会对响应中的指定实体进行标识解析和完整对象具体化。