I'm using EF in my MVC project. In my project I need to use stored procedures. My problem is to use output parameter with stored procedures. I have no idea how to do that
Output parameter is a parameter whose value is passed out of the stored procedure/function module, back to the calling PL/SQL block. An OUT parameter must be a variable, not a constant. It can be found only on the left-hand side of an assignment in the module.
The Output Parameters in Stored Procedures are used to return some value or values. A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.
The easy way is to right-click on the procedure in Sql Server Management Studio (SSMS), select 'Execute stored procedure..." and add values for the input parameters as prompted. SSMS will then generate the code to run the procedure in a new query window, and execute it for you.
When you create your entity model, you should make sure that you include stored procedures. Then, create Function Imports for them:
That's it. Now you can use that in code.
using (MyEntities myContext = new MyEntities ()) { System.Data.Objects.ObjectParameter output = new System.Data.Objects.ObjectParameter("OutputParameterName", typeof(int)); myContext.GetCustomerCount(output); Console.WriteLine(output.Value); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With