EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework
原文地址: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 here) 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);
}
}
EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework的更多相关文章
- 【spring mvc】spring mvc POST方式接收单个字符串参数,不加注解,接收到的值为null,加上@RequestBody,接收到{"uid":"品牌分类大”},加上@RequestParam报错 ---- GET方式接收单个参数的方法
spring mvc POST方式 接收单个参数,不加任何注解,参数名对应,接收到的值为null spring mvc POST方式 接收单个参数,加上@RequestBody,接收到参数格式:{&q ...
- [转]Easy Stored Procedure Output Oracle Select
本文转自:http://www.oraclealchemist.com/oracle/easy-stored-procedure-output/ I answered a question on a ...
- EF报错:对一个或多个实体的验证失败(Entity Framework 强制转换失败数据异常处理方法)
1.使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败.有关详细信 ...
- EF 6 调用存储过程时返回多结果集和OUTPUT参数问题
原文地址:http://q.cnblogs.com/q/56836/ 各位大侠,提问一个关于EF6调用存储过程时返回多结果集和OUTPUT参数问题 目前已经可以调用存储过程并且可以返回多个结果集. 但 ...
- springmvc 传递和接收数组参数
java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...
- How to call a stored procedure in EF Core 3.0 via FromSqlRaw(转载)
问: I recently migrated from EF Core 2.2 to EF Core 3.0. Unfortunately, I haven't found a way to call ...
- EF Power Tools参数不正确的解决方法
在Visual Studio 2010安装了EF Power Tools Beta 3之后,希望根据本地现有数据库模型来生成基于Entity Framework Code First的代码时,经常出现 ...
- struts2视频学习笔记 11-12(动态方法调用,接收请求参数)
课时11 动态方法调用 如果Action中存在多个方法时,可以使用!+方法名调用指定方法.(不推荐使用) public String execute(){ setMsg("execute&q ...
- Struts2中Action接收参数的方法
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt112 Struts2中Action接收参数的方法主要有以下三种: 1.使用A ...
随机推荐
- 【代码优化】equals深入理解
覆盖equals时,遵守通用约定 对equal方法的覆盖看起来非常easy,可是有很多情况是容易导致错误,最好的避免这些错误的办法 就是不覆盖equals方法. 必须遵循的原则: 自反性--对于不论什 ...
- MSSQL row_number简单使用语法
MSSQL row_number简单使用语法 select * from ( select row_number() over(partition by threadid order by date ...
- LVM命令摘要
命令 描述 物理卷(PV) pvcreate 创建LVM磁盘 #pvcreate /dev/sdb pvdisplay 显示卷组中的物理卷信息 pvchange 设置PV的性能,允许或拒绝 ...
- Python基础----函数
1.作用域: 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'wupeiqi' print name 下面的结论对吗? 外层变量,可以被内 ...
- [Linux]三种方案在Windows系统下安装ubuntu双系统(转)
在学习linux的过程中,ubuntu无疑是初学者的最佳选择. 下面来列举给Windows系统安装ubuntu双系统的三种方法. 一.虚拟机安装(不推荐) 使用工具:Vmware 如果不是因为迫不得已 ...
- JAVA通过url获取页面内容
String address = "http://sports.sina.com.cn/nba/live.html?id=2015050405"; URL url = new UR ...
- ReportViewer2010冻结行列
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NewTrackingVer ...
- 运维监控利器smokeping 500报错处理
检查apache日志发现有以下错误: No such file or directory: exec of '/usr/local/smokeping/htdocs/smokeping.cgi' fa ...
- iOS 网络与多线程--7.Performselector消息处理方法
创建一个IOSApp类 IOSApp.h文件 #import <Foundation/Foundation.h> @interface IOSApp : NSObject // 1.添加一 ...
- thrift之TTransport类体系原理及源码详细解析1-类结构和抽象基类
本章主要介绍Thrift的传输层功能的实现,传输的方式多种多样,可以采用压缩.分帧等,而这些功能的实现都是相互独立,和上一章介绍的协议类实现方式比较雷同,还是先看看这部分的类关系图,如下: 由上面的类 ...