Rahul Rahul - 7 months ago 63
SQL Question

How to Execute Stored Procedure in MVC

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.

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.

Comments