Freigeben über


Gewusst wie: Ausführen einer Abfrage, die PrimitiveType-Ergebnisse zurückgibt (EntityClient)

In diesem Thema wird anhand eines Beispiels dargestellt, wie ein Befehl für ein Entity Data Model (EDM) unter Verwendung von EntityCommand ausgeführt und die PrimitiveType-Ergebnisse mit EntityDataReader abgerufen werden.

So führen Sie den Code in diesem Beispiel aus

  1. Fügen Sie das AdventureWorks Sales-Modell dem Projekt hinzu, und konfigurieren Sie das Projekt für die Verwendung von Entity Framework. Gehen Sie dazu folgendermaßen vor:

  2. Fügen Sie der Codepage Ihrer Anwendung die folgenden using-Anweisungen (Imports in Visual Basic) hinzu:

    Imports System
    Imports System.Collections.Generic
    Imports System.Collections
    Imports System.Data.Common
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Data.EntityClient
    Imports System.Data.Metadata.Edm
    Imports System.IO
    ' Add AdventureWorksModel prepended with the root namespace for the project.
    'Imports ProjectName.AdventureWorksModel
    
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Data.Common;
    using System.Data;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data.EntityClient;
    using AdventureWorksModel;
    using System.Data.Metadata.Edm;
    

Beispiel

Um den Code zu testen, übergeben Sie der ExecutePrimitiveTypeQuery-Funktion die folgende Abfrage als Argument:

"SELECT VALUE AVG(p.ListPrice) FROM AdventureWorksEntities.Product as p"

Die obige Abfrage gibt das PrimitiveType-Ergebnis zurück.

Sub ExecutePrimitiveTypeQuery(ByVal esqlQuery As String)
    If (esqlQuery.Length = 0) Then
        Console.WriteLine("The query string is empty.")
        Return
    End If

    Using conn As EntityConnection = New EntityConnection("name=AdventureWorksEntities")
        conn.Open()

        Try
            ' Create an EntityCommand.
            Using cmd As EntityCommand = conn.CreateCommand()
                cmd.CommandText = esqlQuery
                ' Execute the command.
                Using reader As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
                    Do While (reader.Read())
                        Dim record As IExtendedDataRecord = CType(reader, IExtendedDataRecord)
                        ' For PrimitiveType 
                        ' the record contains exactly one field.
                        Dim fieldIndex As Integer
                        fieldIndex = 0
                        Console.WriteLine("Value: " + record.GetValue(fieldIndex))
                    Loop
                End Using
            End Using
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        End Try
        conn.Close()
    End Using
End Sub
static void ExecutePrimitiveTypeQuery(string esqlQuery)
{
    if (esqlQuery.Length == 0)
    {
        Console.WriteLine("The query string is empty.");
        return;
    }

    using (EntityConnection conn =
        new EntityConnection("name=AdventureWorksEntities"))
    {
        conn.Open();

        try
        {
            // Create an EntityCommand.
            using (EntityCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = esqlQuery;
                // Execute the command.
                using (EntityDataReader rdr =
                    cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    // Start reading results.
                    while (rdr.Read())
                    {
                        IExtendedDataRecord record = rdr as IExtendedDataRecord;
                        // For PrimitiveType 
                        // the record contains exactly one field.
                        int fieldIndex = 0;
                        Console.WriteLine("Value: " + record.GetValue(fieldIndex));
                    }
                }
            }
        }
        catch (EntityException ex)
        {
            Console.WriteLine(ex.ToString());
        }
        conn.Close();
    }
}

Siehe auch

Konzepte

Entity SQL-Referenz

Weitere Ressourcen

Arbeiten mit EntityClient (Entity Framework-Aufgaben)