How to Handle Stored Procedure Output Parameter with ADO.NET Entity Framework

No.of Views803
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  11 Sep 2011 03:09:50
Tag : ADO.NET Entity Framework , How to
In this article, I'm going to explain how to handle Output Parameter in Stored Procedure with Entiry Framework.The Entity Framework is getting popular and many developers love it.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, I'm going to explain how to handle Output Parameter in Stored Procedure with Entiry Framework.The Entity Framework is getting popular and many developers love it.So let’s start coding for that.For demo, I have create a table called ‘Customer’ which contains just two columns CustomerId and CustomerName. Following is the script for that.

/****** Object:  Table [dbo].[Customer]    Script Date: 09/09/2011 00:18:33 ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
CREATE TABLE [dbo].[Customer](
    [CustomerId] [int] NOT NULL,
    [CustomerName] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
    [CustomerId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO

Now as our table are ready. Let’s create a stored procedure which will return the number of records as output parameter. Following is script for that.

/****** Object:  StoredProcedure [dbo].[esp_GetCustomerCount]    Script Date: 09/09/2011 00:20:21 ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
CREATE PROCEDURE [dbo].[esp_GetCustomerCount]
    @CustomerCount INT OUTPUT
AS
    select @CustomerCount=COUNT(customerId) from Customer
GO

Now our database part is ready so its time to create a entity model. So first I have created a console application and I added a new entity model via project-> right click-> Add new item and selected ADO.NET Entity Model like following.

 

 

Image Loading

Once I clicked add a wizard will start asking for choosing model contents like following.

Image Loading

Here I have selected Generate from database and clicked next it will ask for connection string. I have selected connection string and click next it will ask to select object of database. Here I have selected tables and  stored procedure which we have created earlier like following.

Image Loading

Now once we have done our model creation its time to create a function import for store procedure. So do that we need to first open Experiment.edmx in visual studio like below.

Image Loading

Once you click Model browser it will  reopen model browser in right side of your edmx like following.

Image Loading

Now in your store you need to expand store part and select stored procedure and click Add function import like following to create function for stored procedure.

Image Loading

Once you click Add function import a dialog box will open to select stored procedure and return type like following.

Image Loading

Here I have changed name of function called ‘GetCustomerCount’ now once you click OK it will create a function called ‘GetCustomerCount’ in entity framework model. Now its time to use that in code. Following is the code from where can find the output parameter value.

using System;
 
namespace ExperimentConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ExperimentEntities myContext = new ExperimentEntities())
            {
                System.Data.Objects.ObjectParameter output = new System.Data.Objects.ObjectParameter("CustomerCount", typeof(int));
                myContext.GetCustomerCount(output);
                Console.WriteLine(output.Value);
            }
        }
    }
}

Here in above code you can see that I have created a object parameter and passed that to function as output parameter is there. Once this function will complete execution it will return value and you can get value with .value property. I have printed that in console application. Following is the output as expected as I have added four records in database.

Image Loading

So that’s it. It’s very easy to use output parameter. Hope you liked it.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
    This article will cover the code-only approach, which provides developers the ability to use the Entity Framework using POCO entities and without an EDMX file. This approach lets developers view code as their model.
    Published Date : 20/Jul/2010
    In Model first approach, we will be creating the EDM, conceptual model, first with respect to the business, and keep evolute the EDM till it fits for the business and then generate the database schema based on the EDM. At least it makes easier to limit the huge changes.
    Published Date : 20/Jul/2010
    There are lots of ways to implement repository pattern but with ado.net entity model you can create repository pattern within 10 to 15 minutes.
    Published Date : 04/Jun/2010
    Microsoft .net framework 4 have usefull enhancements in there Data-services model After the WCf For quite long Microsoft .net Providing such useful services fro client and server side features One of Most Beautful enhancements
    Published Date : 16/May/2010
    Introduction to ADO.NET Data service
    Published Date : 16/Feb/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top