How to Execute Stored Procedure in MVC

asp.net-mvc asp.net-mvc-3 entity-framework sql sql-server

Question

I want to Execute the Stored Procedure in My MVC application.

I am using Database First approach & I have created Some stored procedure like::

CREATE PROCEDURE [dbo].[TestProcedure]
@parameter1 int
 AS
set @parameter1 = 1
return @parameter1

And I am using some code in My MVC action like this::

 private CueEntities db = new CueEntities();
     public ActionResult ExecuteProcedure()
            {
                var parameter1=new SqlParameter("parameter1",1);
                var test = db.Database.SqlQuery<CueEntities>("exec TestProcedure @parameter1",parameter1);
                var check=db.TestProcedure(1);
                return Json(test);

            }

But I am not able to get the Returned value & can't able to know that wether the Stored procedure is Executed or not. Please Help me on this ASAP.

1
-1
11/29/2013 11:28:32 AM

Accepted Answer

I have Executed the Stored Procedure like, 1) For only Input parameters

var check=db.TestProcedure(1);

2) For Input as well as Output Parameters in Stored Procedures,

 public ActionResult GenerateDocumentInvoice(int CompanyID, int UserID, string Documents)
 {
     var Result = new ObjectParameter("Result", new char());
     var exec = db.GenerateInvoice(CompanyID, UserID, Documents, Result);
     return Json(3);
 }

where Input Parameters in Stored Procedure are:: CompanyID, UserID & Documents While Result is the Output Parameter.

1
4/28/2016 9:58:04 AM

Popular Answer

After you import the procedure into your edmx, change your Action method like below:

public ActionResult ExecuteProcedure()
{
   using(var  db = new CueEntities())
   {
     var parameter = 1;
     var query =  db.Database.SqlQuery<TestProcedure>("TestProcedure @parameter1", 
                    new  SqlParameter("@parameter1", parameter)).ToList();          
        return Json(query,JsonRequestBehavior.AllowGet);     
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow