Na stronie kodowej aplikacji dodaj następujące using dyrektywy (Imports w Visual Basic):
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
Zaimportuj procedurę GetStudentGrades składowaną i określ CourseGrade jednostki jako typ zwracany. Aby uzyskać informacje na temat importowania procedury składowanej, zobacz Instrukcje: importowanie procedury składowanej.
Przykład
Poniższy kod wykonuje procedurę GetStudentGrades składowaną, w której StudentId jest wymaganym parametrem. Wyniki są następnie odczytywane przez element 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
Dołącz do serii meetup, aby tworzyć skalowalne rozwiązania sztucznej inteligencji oparte na rzeczywistych przypadkach użycia z innymi deweloperami i ekspertami.