- Create a stored procedure in database
After expanding the database follow this:
Programmability>Stored Procedures>Right click on it and select New Stored Procedure
Here write codes to create a procedure. I created a simple procedure like following:
My procedure’s name is GetProducts which will take a parameter named pricerange of type int.
Click Execute button on the top and the procedure will be created.
To test the procedure I executed it like this:
I passed the price range value 20 and the command returned all the products having price value below 20.
- Create a class for storing the resultFor holding the result value I created a result class like this in model folder:
- Call the procedure from controllerI wrote a simple controller function like this:
ProjectDbContext is my DbContext class. priceRangeParam is to take my sql parameter. GetProducts is the procedure name created in sql database. The result will be treated as object of my ResultForProduct class which will take only the price value of the returned data. I passed it to view through viewbag and showed it like this:
That’s it! 🙂
(Visited 34 times, 1 visits today)