Share via


DataServiceContext.Execute Metode

Definisi

Mengirim permintaan ke layanan data untuk menjalankan URI tertentu.

Overload

Execute<T>(DataServiceQueryContinuation<T>)

Mengirim permintaan ke layanan data untuk mengambil halaman data berikutnya dalam hasil kueri berhala.

Execute<TElement>(Uri)

Mengirim permintaan ke layanan data untuk menjalankan URI tertentu.

Execute<T>(DataServiceQueryContinuation<T>)

Mengirim permintaan ke layanan data untuk mengambil halaman data berikutnya dalam hasil kueri berhala.

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)

Jenis parameter

T

Jenis yang dikembalikan oleh kueri.

Parameter

continuation
DataServiceQueryContinuation<T>

Objek DataServiceQueryContinuation<T> yang mewakili halaman data berikutnya untuk dikembalikan dari layanan data.

Mengembalikan

Respons yang berisi halaman data berikutnya dalam hasil kueri.

Pengecualian

Ketika kesalahan dimunculkan baik selama eksekusi permintaan atau ketika mengonversi konten pesan respons menjadi objek.

Keterangan

Objek yang disediakan DataServiceQueryContinuation<T> berisi URI yang, saat dijalankan, mengembalikan halaman data berikutnya dalam hasil kueri.

Berlaku untuk

Execute<TElement>(Uri)

Mengirim permintaan ke layanan data untuk menjalankan URI tertentu.

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)

Jenis parameter

TElement

Jenis yang dikembalikan kueri.

Parameter

requestUri
Uri

URI tempat permintaan kueri akan dikirim. URI mungkin URI layanan data yang valid. Dapat berisi parameter kueri $.

Mengembalikan

IEnumerable<TElement>

Hasil operasi kueri.

Pengecualian

Ketika respons tidak diterima dari permintaan ke requestUri.

Kapan requestUri adalah null.

Ketika requestUri bukan URI yang valid untuk layanan data.

Ketika kesalahan dimunculkan baik selama eksekusi permintaan atau ketika mengonversi konten pesan respons menjadi objek.

Layanan data mengembalikan kesalahan HTTP 404: Sumber Daya Tidak Ditemukan.

Contoh

Contoh ini menggunakan perulangan do…while untuk memuat Customers entitas dari hasil halaman dari layanan data. Metode Execute ini dipanggil dengan menggunakan URI tautan berikutnya untuk menerima halaman data berikutnya.

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

Keterangan

Metode Execute ini digunakan untuk mengkueri layanan data oleh URI; metode menyebabkan permintaan HTTP GET dikeluarkan ke layanan data. URI permintaan yang ditentukan dapat bersifat absolut atau relatif.

requestUri Jika absolut, metode ini memvalidasi apakah URI menunjuk ke layanan data yang sama yang ditentukan saat membuat DataServiceContext. requestUri Jika relatif, metode ini menanggalkan garis miring terkemuka dan menambahkan requestUri ke apa yang disediakan saat membuat DataServiceContext. Garis miring ditambahkan setelah URI diteruskan ke DataServiceContext konstruktor, jika belum ada.

Ketika metode ini kembali, semua respons HTTP untuk permintaan telah dibaca dari aliran jaringan, tetapi respons tidak akan diproses; tidak ada resolusi identitas atau materialisasi objek. Resolusi identitas dan materialisasi objek penuh tidak terjadi untuk entitas tertentu dalam respons hingga dijumlahkan.

Lihat juga

Berlaku untuk