星期日, 4月 28, 2019

[EF] Store Procedure - Output 參數

閱讀 設計工具的查詢預存程序 時,才發現到之前筆記 - [EF] 使用 Store Procedure 沒有記錄到 Store Procedure Output 參數使用方式,再紀錄一下

Store Procedure - GetDepartmentName TSQL
CREATE PROCEDURE [dbo].[GetDepartmentName]
  @ID int,
  @Name nvarchar(50) OUTPUT
AS
  SELECT @Name = Name 
  FROM Department
  WHERE DepartmentID = @ID
GO
C# Code
// ObjectParameter 所在 namespace
using System.Data.Entity.Core.Objects;

namespace EFStoreProcedureOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SchoolEntities context = new SchoolEntities())
            {
                ObjectParameter op = new ObjectParameter("Name", typeof(String));
                context.GetDepartmentName(1, op);
                Console.WriteLine($"ObjectParameter.Name:{op.Name}");
                Console.WriteLine($"ObjectParameter.ParameterType:{op.ParameterType}");
                Console.WriteLine($"ObjectParameter.Value:{op.Value}");
            }
        }
    }
}
Untitled

沒有留言:

張貼留言