从存储过程获取返回值 [英] Get return value from stored procedure

查看:124
本文介绍了从存储过程获取返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架5与code第一种方法。我需要阅读从存储过程的返回值;我已经读输出参数和发送的输入参数,但我不知道如何读的返​​回值。

这可能吗?

下面是我用来调用存储过程code:

  VAR outParam =新的SqlParameter();
outParam.ParameterName =@StatusLog;
outParam.SqlDbType = SqlDbType.NVarChar;
outParam.Size = 4000;
outParam.Direction = ParameterDirection.Output;变量code =新的SqlParameter();
code.ParameterName =@ code;
code.Direction = ParameterDirection.Input;
code.SqlDbType = SqlDbType.VarChar;
code.Size = 20;
code.Value =123;VAR数据= _context.Database.SqlQuery<项目>(EXEC spItemData @ code,@StatusLog OUT,code,outParam);VAR的结果= data.FirstOrDefault();


解决方案

我找到了!
可以读取与具有以这种方式被使用的输出参数的返回值:

  //定义一个新的输出参数
VAR回报code =新的SqlParameter();
返回code.ParameterName =@返回code;
返回code.SqlDbType = SqlDbType.Int;
返回code.Direction = ParameterDirection.Output;//分配返回code到新的输出参数,并把它传递给SP
VAR数据= _context.Database.SqlQuery<项目>(EXEC @返回code = spItemData @ code,@StatusLog OUT,返回code,code,outParam);

I'm using Entity Framework 5 with the Code First approach. I need to read the return value from a stored procedure; I am already reading output parameters and sending input parameters, but I don't know how to read the return value.

Is it possible?

Here is the code that I use to call the stored procedure:

var outParam = new SqlParameter();
outParam.ParameterName = "@StatusLog";
outParam.SqlDbType = SqlDbType.NVarChar;
outParam.Size = 4000;
outParam.Direction = ParameterDirection.Output;

var code = new SqlParameter();
code.ParameterName = "@Code";
code.Direction = ParameterDirection.Input;
code.SqlDbType = SqlDbType.VarChar;
code.Size = 20;
code.Value = "123";

var data = _context.Database.SqlQuery<Item>("exec spItemData @Code, @StatusLog OUT", code, outParam);

var result = data.FirstOrDefault();

解决方案

I found it! I can read the return value with an output parameter that has to be used in this way:

// define a new output parameter
var returnCode = new SqlParameter();
returnCode.ParameterName = "@ReturnCode";
returnCode.SqlDbType = SqlDbType.Int;
returnCode.Direction = ParameterDirection.Output;

// assign the return code to the new output parameter and pass it to the sp
var data = _context.Database.SqlQuery<Item>("exec @ReturnCode = spItemData @Code, @StatusLog OUT", returnCode, code, outParam);

这篇关于从存储过程获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆