Postup: Provedení dotazu, který vrátí výsledky typu PrimitiveType

Toto téma ukazuje, jak spustit příkaz proti konceptuálnímu modelu pomocí a EntityCommandjak načíst PrimitiveType výsledky pomocí .EntityDataReader

Spuštění kódu v tomto příkladu

  1. Přidejte do projektu model AdventureWorks Sales Model a nakonfigurujte projekt tak, aby používal Entity Framework. Další informace naleznete v tématu Postupy: Použití Průvodce datovým modelem entity.

  2. Na znakovou stránku aplikace přidejte následující using příkazy (Imports v jazyce Visual Basic):

    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 System.Data.Metadata.Edm;
    
    Imports System.Collections.Generic
    Imports System.Collections
    Imports System.Data.Common
    Imports System.Data
    Imports System.IO
    Imports System.Data.SqlClient
    Imports System.Data.EntityClient
    Imports System.Data.Metadata.Edm
    
    

Příklad

Tento příklad spustí dotaz, který vrátí PrimitiveType výsledek. Pokud funkci předáte jako argument ExecutePrimitiveTypeQuery následující dotaz, zobrazí funkce průměrnou ceníkovou cenu všech Products:

SELECT VALUE AVG(p.ListPrice) FROM AdventureWorksEntities.Products as p

Pokud předáte parametrizovaný dotaz, například následující, EntityParameter objekty do Parameters vlastnosti objektu EntityCommand .

CASE WHEN AVG({@score1,@score2,@score3}) < @total THEN TRUE ELSE FALSE END
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();

        // 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));
                }
            }
        }
        conn.Close();
    }
}
Private Shared Sub ExecutePrimitiveTypeQuery(ByVal esqlQuery As String)
    If esqlQuery.Length = 0 Then
        Console.WriteLine("The query string is empty.")
        Exit Sub
    End If

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

        ' Create an EntityCommand. 
        Using cmd As EntityCommand = conn.CreateCommand()
            cmd.CommandText = esqlQuery
            ' Execute the command. 
            Using rdr As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
                ' Start reading results. 
                While rdr.Read()
                    Dim record As IExtendedDataRecord = TryCast(rdr, IExtendedDataRecord)
                    ' For PrimitiveType 
                    ' the record contains exactly one field. 
                    Dim fieldIndex As Integer = 0
                    Console.WriteLine("Value: " & record.GetValue(fieldIndex))
                End While
            End Using
        End Using
        conn.Close()
    End Using
End Sub

Viz také