从实体框架的存储过程ASP.NET MVC4回报输出 [英] ASP.NET MVC4 return output from a stored procedure with Entity Framework

查看:154
本文介绍了从实体框架的存储过程ASP.NET MVC4回报输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图从一个存储过程

  ALTER PROCEDURE [DBO]。[sp_getrandomnumber] $ B一个简单的输出$ b @randoms INT输出
AS
开始
组@randoms = 12345

和我的MVC

 公众的ActionResult指数()
{
VAR QRY = db.sp_getrandomnumber(REF偶合​​);
......
返回查看();
}
}



但我编译时得到错误 VAR QRY 部分说以下




没有过载方法'sp_getrandomnumber'需要1个参数



偶合不存在于当前上下文


存在的名称


我尝试下面这个教程
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures的.aspx



提前
碎甲弹


解决方案

您的问题是,你的存储过程的输出是一个int。



请参阅从斯科特谷的文章下面的报价。



的LINQ to SQL映射出,在存储过程的参数作为基准参数(ref关键字),和值类型声明参数为空。



所以,你需要确保你声明偶合作为一个可空int类型。



更改SP调用

 公众的ActionResult指数()
{从存储过程
输出// @randoms INT。
诠释?偶合= NULL;

// QRY将包含一个选择,如果你有一个在存储过程。
VAR QRY = db.sp_getrandomnumber(REF偶合​​);

//偶合为12345

返回查看();
}



查找另一个例子的这里


I been trying to get a simple output from a stored procedure

ALTER PROCEDURE [dbo].[sp_getrandomnumber]
        @randoms int output
AS
begin
  set @randoms =12345
end

and my mvc

    public ActionResult Index()
    {
        var qry = db.sp_getrandomnumber(ref randoms); 
      ......
     return View();
    }
}

but when I compile I get errors on the var qry section saying the following

No overload for method 'sp_getrandomnumber' takes 1 arguments

The name 'randoms' does not exist in the current context

I tried following this tutorial http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

many thanks in advance Hesh

解决方案

Your problem is that your SPROC output is an int.

See the following quote from Scott Gu's article.

"LINQ to SQL maps "out" parameters in SPROCs as reference parameters (ref keyword), and for value types declares the parameter as nullable."

So you need to make sure you're declaring randoms as a nullable int.

Change your sp call to

public ActionResult Index()
{   
    // @randoms int output from SPROC.
    int? randoms = null;

    // qry would contain a select if you had one in the SPROC.
    var qry = db.sp_getrandomnumber(ref randoms); 

    // randoms is 12345

    return View();
}

Find another example here.

这篇关于从实体框架的存储过程ASP.NET MVC4回报输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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