ASP.NET MVC4使用Entity Framework从存储过程返回输出

时间:2021-01-17 02:15:39

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

和我的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

但是当我编译时,我在var qry部分得到错误,说下面的内容

No overload for method 'sp_getrandomnumber' takes 1 arguments

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

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

当前上下文中不存在“randoms”这个名称

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

我尝试过本教程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

非常感谢Hesh

1 个解决方案

#1


1  

Your problem is that your SPROC output is an int.

您的问题是您的SPROC输出是一个int。

See the following quote from Scott Gu's article.

请参阅Scott Gu的文章中的以下引用。

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

“LINQ to SQL将”SPROC中的参数“映射”为参考参数(ref关键字),对于值类型,将参数声明为可为空。“

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

所以你需要确保你将randoms声明为可以为空的int。

Change your sp call to

将您的sp呼叫更改为

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.

在这里找另一个例子。

#1


1  

Your problem is that your SPROC output is an int.

您的问题是您的SPROC输出是一个int。

See the following quote from Scott Gu's article.

请参阅Scott Gu的文章中的以下引用。

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

“LINQ to SQL将”SPROC中的参数“映射”为参考参数(ref关键字),对于值类型,将参数声明为可为空。“

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

所以你需要确保你将randoms声明为可以为空的int。

Change your sp call to

将您的sp呼叫更改为

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.

在这里找另一个例子。