방법: EntityCommand를 사용하여 매개 변수 있는 저장 프로시저 실행(EntityClient)

이 항목에서는 EntityCommand를 사용하여 매개 변수가 있는 저장 프로시저를 실행하는 방법에 대한 예제를 제공합니다. 이 예제에서는 저장 프로시저와 방법: 저장 프로시저로 모델 정의(Entity Framework) 항목에서 구현된 데이터 모델을 사용합니다. 프로젝트 구성에 대한 자세한 내용과 개체 서비스를 사용하여 저장 프로시저를 실행하는 방법에 대한 예제를 보려면 방법: 저장 프로시저를 사용하여 쿼리 실행(Entity Framework)을 참조하십시오.

예제

다음 코드는 SalesOrderHeaderId가 필수 매개 변수인 경우 매개 변수가 있는 저장 프로시저를 실행합니다. 그런 다음 EntityDataReader에서 결과를 읽습니다.

Using conn As EntityConnection = New EntityConnection("name=AdventureWorksEntities")
    conn.Open()

    Try

        ' Create an EntityCommand.
        Using cmd As EntityCommand = conn.CreateCommand()
            cmd.CommandText = "AdventureWorksEntities.GetOrderDetails"
            cmd.CommandType = CommandType.StoredProcedure
            Dim param As New EntityParameter()
            param.Value = "43659"
            param.ParameterName = "SalesOrderHeaderId"
            cmd.Parameters.Add(param)

            Using rdr As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
                Do While rdr.Read
                    ' Read the results returned by the stored procedure.
                    Console.WriteLine("Header#: {0} " & _
                    "Order#: {1} ProductID: {2} Quantity: {3} Price: {4}", _
                    rdr.Item(0), rdr.Item(1), rdr.Item(2), rdr.Item(3), rdr.Item(4))
                Loop
            End Using
        End Using
    Catch ex As EntityException
        Console.WriteLine(ex.ToString())
    End Try
    conn.Close()
End Using
using (EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities"))
{
    conn.Open();
    try
    {
        // Create an EntityCommand.
        using (EntityCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = "AdventureWorksEntities.GetOrderDetails";
            cmd.CommandType = CommandType.StoredProcedure;
            EntityParameter param = new EntityParameter();
            param.Value = "43659";
            param.ParameterName = "SalesOrderHeaderId";
            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("Header#: {0} " +
                    "Order#: {1} ProductID: {2} Quantity: {3} Price: {4}",
                    rdr[0], rdr[1], rdr[2], rdr[3], rdr[4]);
                }
            }
        }
    }
    catch (EntityException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    conn.Close();
}

참고 항목

기타 리소스

EntityClient 사용(Entity Framework 작업)