Udostępnij za pośrednictwem


How to: Execute a Query that Returns PrimitiveType Results (EntityClient)

This topic provides an example of how to execute a command against an Entity Data Model (EDM) by using EntityCommand, and retrieve the PrimitiveType results by using EntityDataReader.

To run the code in this example

  1. Add the AdventureWorks Sales Model to your project and configure your project to use the Entity Framework. To do this, do one of the following:

  2. In the code page for your application, add the following using statements (Imports in Visual Basic):

    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;
    

Example

To test the code, pass the following query as an argument to the ExecutePrimitiveTypeQuery function:

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

The query above returns the PrimitiveType result.

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();
    }
}

See Also

Concepts

Entity SQL Reference

Other Resources

Working with EntityClient (Entity Framework Tasks)