Procedimiento para ejecutar un procedimiento almacenado parametrizado mediante EntityCommand

En este tema se muestra cómo ejecutar un procedimiento almacenado parametrizado usando la clase EntityCommand.

Para ejecutar el código de este ejemplo

  1. Agregue el School Model al proyecto y configúrelo para usar Entity Framework. Para más información, consulte Cómo: Usar el Asistente para Entity Data Model.

  2. En la página de código de la aplicación, agregue las instrucciones using siguientes (Imports en 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
    
    
  3. Importe el procedimiento almacenado GetStudentGrades y especifique entidades CourseGrade como tipo de valor devuelto. Para obtener información sobre cómo importar un procedimiento almacenado, vea Cómo: Importar un procedimiento almacenado.

Ejemplo

El código siguiente ejecuta el procedimiento almacenado GetStudentGrades donde StudentId es un parámetro necesario. Los resultados los lee después una clase EntityDataReader.

using (EntityConnection conn =
    new EntityConnection("name=SchoolEntities"))
{
    conn.Open();
    // Create an EntityCommand.
    using (EntityCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "SchoolEntities.GetStudentGrades";
        cmd.CommandType = CommandType.StoredProcedure;
        EntityParameter param = new EntityParameter();
        param.Value = 2;
        param.ParameterName = "StudentID";
        cmd.Parameters.Add(param);

        // Execute the command.
        using (EntityDataReader rdr =
            cmd.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            // Read the results returned by the stored procedure.
            while (rdr.Read())
            {
                Console.WriteLine("ID: {0} Grade: {1}", rdr["StudentID"], rdr["Grade"]);
            }
        }
    }
    conn.Close();
}
Using conn As New EntityConnection("name=SchoolEntities")
    conn.Open()
    ' Create an EntityCommand. 
    Using cmd As EntityCommand = conn.CreateCommand()
        cmd.CommandText = "SchoolEntities.GetStudentGrades"
        cmd.CommandType = CommandType.StoredProcedure
        Dim param As New EntityParameter()
        param.Value = 2
        param.ParameterName = "StudentID"
        cmd.Parameters.Add(param)

        ' Execute the command. 
        Using rdr As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            ' Read the results returned by the stored procedure. 
            While rdr.Read()
                Console.WriteLine("ID: {0} Grade: {1}", rdr("StudentID"), rdr("Grade"))
            End While
        End Using
    End Using
    conn.Close()
End Using

Consulte también