Share via


DataServiceContext.Execute 方法

定義

將要求傳送到資料服務來執行特定的 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; 它能包含 $ 查詢參數。

傳回

IEnumerable<TElement>

查詢作業的結果。

例外狀況

未收到 requestUri 要求的回應時。

requestUrinull 時。

requestUri 不是資料服務的有效 URI 時。

在執行要求期間或將回應訊息的內容轉換為物件時引發錯誤時。

數據服務會傳回 HTTP 404:找不到資源錯誤。

範例

本範例使用 do…while 迴圈,從資料服務的分頁結果載入 Customers 實體。 透過使用下一個連結 URI 呼叫 Execute 方法以接收下一頁資料。

// 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。

如果 requestUri 是絕對 URI,這個方法會驗證 URI 是否指向建構 DataServiceContext 時所指定的相同資料服務。 如果 requestUri 是相對,此方法會移除任何前導斜線並將 requestUri 附加到建構 DataServiceContext 時提供的路徑。 在傳遞至 DataServiceContext 建構函式的 URI 後面會附加斜線 (若沒有的話)。

當此方法傳回時,已從網路資料流讀取要求的所有 HTTP 回應,但尚未處理回應;不會識別解析或將物件具體化。 除非回應中的指定之實體經過列舉,否則不會對該實體進行任何識別解析或完整物件具體化。

另請參閱

適用於