Sdílet prostřednictvím


Postupy: Spuštění parametrizované uložené procedury pomocí EntityCommand

Toto téma ukazuje, jak spustit parametrizovanou uloženou proceduru EntityCommand pomocí třídy.

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

  1. Přidejte do projektu školní 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 znakové stránce pro vaši aplikaci přidejte následující using direktivy (Imports v jazyce Visual Basic):

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    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. Importujte uloženou proceduru GetStudentGrades a zadejte CourseGrade entity jako návratový typ. Informace o importu uložené procedury naleznete v tématu Postupy: Import uložené procedury.

Příklad

Následující kód spustí uloženou proceduru GetStudentGrades , kde StudentId je povinný parametr. Výsledky se pak čtou pomocí funkce 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

Viz také