Nasıl yapılır: PrimitiveType Sonuçları Döndüren Bir Sorgu Yürütme

Bu konu başlığında, kullanarak kavramsal modele EntityCommandkarşı komut yürütme ve kullanarak sonuçları alma PrimitiveType adımları gösterilmektedir EntityDataReader.

Bu örnekteki kodu çalıştırmak için

  1. Projenize AdventureWorks Satış Modeli'ni ekleyin ve projenizi Entity Framework kullanacak şekilde yapılandırın. Daha fazla bilgi için bkz . Nasıl yapılır: Varlık Veri Modeli Sihirbazı'nı kullanma.

  2. Uygulamanızın kod sayfasına aşağıdaki using deyimleri ekleyin (Imports Visual Basic'te):

    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
    
    

Örnek

Bu örnek, sonuç döndüren bir PrimitiveType sorgu yürütür. Aşağıdaki sorguyu işleve ExecutePrimitiveTypeQuery bağımsız değişken olarak geçirirseniz, işlev tüm Productsortalama liste fiyatını görüntüler:

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

Aşağıdaki gibi parametreli bir sorgu geçirirseniz nesnedeki EntityParameterParameters özelliğine EntityCommand nesneler.

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

Ayrıca bkz.