DataServiceContext.Execute 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
向資料服務發送請求以執行特定的 URI。
多載
| 名稱 | Description |
|---|---|
| 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。
何時 requestUri 不是資料服務的有效 URI。
當錯誤發生時,無論是在請求執行過程中,還是當回應訊息內容轉換成物件時。
資料服務會回傳 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 可以是絕對的,也可以是相對的。
若 為 requestUri 絕對,此方法驗證 URI 是否指向建構 DataServiceContext時指定的相同資料服務。 若 為 requestUri 相對,此方法會去除所有前置斜線,並附加 requestUri 於構造 DataServiceContext時提供的斜線。 如果 URI 傳給 DataServiceContext 建構者,則會在 URI 上傳後加上斜線,若尚未存在。
當此方法回傳時,請求的所有 HTTP 回應都已從網路串流中讀取,但回應尚未被處理;沒有身份解析或物件的具體化。 在回應中指定的實體被枚舉前,不會對該實體進行身份解析與完整物件實體化。