ASP.NET MVC4 return output from a stored procedure with Entity Framework

前端 未结 1 1710
生来不讨喜
生来不讨喜 2021-01-26 06:09

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

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


        
相关标签:
1条回答
  • 2021-01-26 06:27

    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.

    0 讨论(0)
提交回复
热议问题