You are on page 1of 3

8/7/2017 How to get stored procedure OUTPUT Parameters in Entity Framework

Open Source .Net


SUNDAY, AUGUST 10, 2014 ONLINE TYPING TOOL

How to get stored procedure OUTPUT Online Malayalam Typing


Tool

Parameters in Entity Framework


Calling stored procedure from Entity framework code first is not an issue. Its
MICROSOFT
explained very well in Entity Framework tutorials as well [Here] , Let see how CERTIFICATIONS
to call custom stored procedure from C#

This is my stored procedure to return total number of employes from database

USE [AdventureWorksDW2008R2]
GO
/****** Object: StoredProcedure [dbo].[getEmployeeCount]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Raju Melveetilpurayil
-- Create date: BLOG ARCHIVE
-- Description: Get total number of employes from AdventureWorksDW2008R2 database
-- ============================================= 2016 (1)
ALTER PROCEDURE [dbo].[getEmployeeCount]
AS 2015 (1)
BEGIN 2014 (5)
SET NOCOUNT ON; August 2014 (3)
How to create CAPTCHA
SELECT COUNT(*)AS EmpCount FROM dbo.DimEmployee in MVC 5
END Paging In MVC 5 with
Entity Framework
-- How to get stored
-- procedure OUTPUT
Parameters in E...

Now we can check how to call above stored procedure from C# July 2014 (1)
March 2014 (1)

using (var db = new EmployeeContext()) 2012 (2)


{ 2011 (6)
var totalNumberOfEmployes = db.Database.SqlQuery<int>("getEmployeeCount").ToList();
2010 (22)
int totalNumber = totalNumberOfEmployes.FirstOrDefault<int>();
} 2009 (11)
2008 (50)

So we can call a stored procedure with some parameter including OUTPUT CONTENTS
parameter
ASP.NET (40)
USE [AdventureWorksDW2008R2] Code Snippets
GO (31) C#.Net (26)
/****** Object: StoredProcedure [dbo].[getEmployeeByPageNumber] Script Date: 08/10/2014 22:25:18
Payment******/
Module (5) API
SET ANSI_NULLS ON Integration (4) State
GO Management (4) Javascript
(3) IP Address (2) SQL SERVER
SET QUOTED_IDENTIFIER ON
2005 (2) Read Email From
GO
ASP.NET (1)
-- =============================================
-- Author: <Raju Melveetilpurayil>
http://makhaai.blogspot.in/2014/08/how-to-get-stored-procedure-output.html 1/3
8/7/2017 How to get stored procedure OUTPUT Parameters in Entity Framework
-- Create date: <Create Date,,> USEFULL BOOKS TO
-- Description: <Get rows by row between limit> BUY
-- =============================================
ALTER PROCEDURE [dbo].[getEmployeeByPageNumber]
-- Add the parameters for the stored procedure here
@Start INT,
@End INT,
@TotalCount INT OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT @TotalCount=COUNT(*) FROM dbo.DimEmployee
SELECT [EmployeeKey]
,[FirstName]
,[LastName]
,[Title] MY PROFILES
,[EmailAddress]
,[DepartmentName] FROM Facebook Page
( Twitter
SELECT ROW_NUMBER() OVER(ORDER BY EmployeeKey) ROW_NUM, * FROM DimEmployee Dotnetobject
) AS K ASP.Net Forums
WHERE ROW_NUM >@Start AND ROW_NUM <=@End CodeProject
DNS
--
--
END FOLLOWERS
Followers (40) Next

Actually the above script I am using for to get data between some limit.. I will
use this same script in my later articles for How to do paging in MVC 5 We need
to pass one SqlParameter parameter and set its direction to
System.Data.ParameterDirection.Output, after executing the function SqlParameter
hold the OUTPUT data.

var spOutput = new SqlParameter {


ParameterName = "@TotalCount",
SqlDbType = System.Data.SqlDbType.BigInt,
Direction = System.Data.ParameterDirection.Output Follow
};

we pass other parameter like normal SqlParameter

var start = new SqlParameter("@Start", limitStart);


var end = new SqlParameter("@End", limitEnd);

db.Database.SqlQuery help us to call raw Sql from entity framework The whole
code

//
// calling stored procedure to get the total result count

var start = new SqlParameter("@Start", limitStart);


var end = new SqlParameter("@End", limitEnd);

//
// setting stored procedure OUTPUT value
// This return total number of rows, and avoid two database call for data and total number of rows
var spOutput = new SqlParameter {
ParameterName = "@TotalCount",
SqlDbType = System.Data.SqlDbType.BigInt,
Direction = System.Data.ParameterDirection.Output
};
//
//calling stored procedure to get paged data.
List<DimEmployee> AllEmpoyees = db.Database.SqlQuery<DimEmployee>(
"getEmployeeByPageNumber @Start,@End,@TotalCount out",
start, end, spOutput)
.ToList();

http://makhaai.blogspot.in/2014/08/how-to-get-stored-procedure-output.html 2/3
8/7/2017 How to get stored procedure OUTPUT Parameters in Entity Framework
//
// setting total number of records
totalCount = int.Parse(spOutput.Value.ToString());

//
//

Happy programming ..

at 2:38 PM

Labels C#, Entity Framework, SQL Server

No comments:
Post a Comment

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

ASP.Net Programmer, C Sharp Programmer, MVC, MVC2, MVC3,London, Southall, Heathrow, XML, Ajax Programmer

http://makhaai.blogspot.in/2014/08/how-to-get-stored-procedure-output.html 3/3

You might also like