博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework...
阅读量:6469 次
发布时间:2019-06-23

本文共 2476 字,大约阅读时间需要 8 分钟。

原文地址:http://blogs.microsoft.co.il/gilf/2010/05/09/how-to-retrieve-stored-procedure-output-parameters-in-entity-framework/ 

 

How to Retrieve Stored Procedure Output Parameters in Entity Framework

 

One question that 

raises from time to 
time in EF forums 
is how you can 
retrieve stored 
procedure output 
parameters in EF 
Function Import. 
This post will show you how to do that.

 

The Stored Procedure

 

In the example I’m going to use the following stored procedure:

 

CREATE PROCEDURE dbo.SchoolBudgetForDateRange
@StartDate DATETIME,
@EndDate DATETIME,
@Sum money output
AS
SET NOCOUNT ON;
SELECT @Sum = SUM(Department.Budget)
FROM Department
WHERE StartDate BETWEEN @StartDate AND @EndDate

 

The stored procedure returns the school budget for a given date. 

By of course the stored procedure could return that calculation 
without using an output parameter.

 

Retrieving Stored Procedure Output Parameter

 

After creating a Function Import (which is explained ) we can 

use the SchoolBudgetforDateRange method with the context we have. 
In order to get an output parameter you need to supply an ObjectParameter 
to the stored procedure call which holds the parameter name and type. 
After the execution of the stored procedure you can retrieve the 
parameter using the Value property of the ObjectParameter
The following code shows how to that exactly what I wrote:

 

static void Main(string[] args)
{
using (SchoolEntities context = new SchoolEntities())
{
var outputParameter = new ObjectParameter(“sum”, typeof(decimal));
context.SchoolBudgetForDateRange(new DateTime(2007, 1, 1),
new DateTime(2008, 1, 1),
outputParameter);
Console.WriteLine(outputParameter.Value);
}
}

 

Summary

 

 

 

Once you need to retrieve output parameters from EF Function Imports

you need to supply an ObjectParameter to hold the output. In the post 
I showed how to do that.

 

----------------------------------------

自我测试

ALTER PROC [dbo].[usp_AddTeacherInfo]

@Name NVARCHAR(50),
@Phone NVARCHAR(50),
@Address NVARCHAR(50),
@Age INT,
@Sum UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
SET NOCOUNT ON
DECLARE @TmpID UNIQUEIDENTIFIER
SET @TmpID = NEWID()
INSERT INTO dbo.Teacher
(ID, Name, Phone, Address, Age )
VALUES ( @TmpID,@Name,@Phone,@Address,@Age)

SET @Sum = @TmpID

SET NOCOUNT OFF
END

 

public void AddTeacherInfo()

{
using (DemoDBEntities entity = new DemoDBEntities())
{
var outputParameter = new ObjectParameter("sum", typeof(Guid));

var dd = entity.usp_AddTeacherInfo("dapeng", "021-99999999", "address222", 86, outputParameter);

}
}

转载地址:http://uedko.baihongyu.com/

你可能感兴趣的文章
Linux Curl命令
查看>>
046 SparlSQL中的函数
查看>>
Zookeeper 的 Lua 绑定(二)
查看>>
-27979 LoadRunner 错误27979 找不到请求表单 Action.c(73): Error -27979: Requested form not found...
查看>>
[LeetCode] Minimum Depth of Binary Tree
查看>>
,net运行框架
查看>>
Java 中 Emoji 的正则表达式
查看>>
Mixin Network第一届开发者大赛作品介绍- dodice, diceos和Fox.one luckycoin
查看>>
安卓Glide(4.7.1)使用笔记 01 - 引入项目
查看>>
AndroidNote
查看>>
中金易云:为出版社找到下一本《解忧杂货店》
查看>>
Flex布局
查看>>
Material Design之 AppbarLayout 开发实践总结
查看>>
Android中的SurfaceView详解
查看>>
Flutter之MaterialApp使用详解
查看>>
DataBinding最全使用说明
查看>>
原生Js交互之DSBridge
查看>>
Matlab编程之——卷积神经网络CNN代码解析
查看>>
白洋淀周末游
查看>>
三篇文章了解 TiDB 技术内幕 —— 说计算
查看>>