LINQ to SQL : Returning Scalar Value from Stored Procedure

LINQ to SQL : Returning Scalar Value from Stored Procedure

  • Comments 10

In LINQ to SQL it is not that easy thing to achieve as compared to other features. Let us assume you have a Stored Procedure like,

Case 1: With Output Parameter

CREATE PROCEDURE [dbo].[GetEmployeeCount]

      @OutVal DateTime OUTPUT

AS

BEGIN

      SELECT @OutVal = GetDate()

END

You need to write code which will look like,

using (TestDBDataContext db = new TestDBDataContext())

{

    //Need a Nullable type here

    //and you need to have some value to it

    DateTime? dt = null;

    var q = db.GetEmployeeCount(ref dt);

 

    Console.WriteLine(dt);               

}

Case 2: With Return (only for Integers)

CREATE PROCEDURE [dbo].[GetEmployeeCountRet]         

AS

BEGIN

      DECLARE @Ret INT

      SELECT @Ret = COUNT(*) FROM Emp

      RETURN @Ret

END

Your code may look like,

using (TestDBDataContext db = new TestDBDataContext())

{

    //For Stored Procedure with Return value (for Integer)

    //returns Int

    var q = db.GetEmployeeCountRet();

 

    Console.WriteLine(q);               

}

You cannot simply say

SELECT COUNT(*) FROM Emp and capture the value in a variable. Because in LINQ to SQL a Stored Procedure either returns ISingleResult<T> or IMultipleResults<T>, so capturing single value becomes very tricky.

So when you have to do it go for Scalar-Valued function

Case 3: Using Scalar-Values Functions

ALTER FUNCTION [dbo].[fn_GetEmployeeCount]()

RETURNS int

AS

BEGIN

      DECLARE @ResultVar int

     

      SELECT @ResultVar = Count(*) FROM Emp

     

      RETURN @ResultVar

END

You code,

using (TestDBDataContext db = new TestDBDataContext())

{

    var q = db.fn_GetEmployeeCount();

 

    Console.WriteLine(q);               

}

Namoskar!!!

Leave a Comment
  • Please add 2 and 8 and type the answer here:
  • Post
  • PingBack from http://microsoft-sharepoint.simplynetdev.com/linq-to-sql-returning-scalar-value-from-stored-procedure/

  • Perfect!  This was EXACTLY the info I was looking for.  Am new to LINQ and was pouding my head into a wall trying to use a paramaterized stored proc to simply return the count of records from a table.

    Switching to a function did the trick.

    Thanks so much for this insight.

  • Thanks a lot ur brilliant

    iam trying this from 2 days

  • Bru, why can't the other blogs get to the point like you do...and effectively!

    Thanks.

  • like the using thing, did not 'get it' to start but now realise what it does, makes things easier to read too !

  • How do use resolve this:

    Cannot implicitly convert 'int?' to 'string'

    Seriously now

  • Thanks for this post! So many other posts online sending me off 10 different directions, your solution was simple and direct, awesome.

  • This was EXACTLY the info I was looking for:)

    www.freedownloadsatellitetv.com

  • Thanks for your support. Iwas looking for this only. I know how to return without using Linq to Sql,i.e.in Sql which returns single value. I was knowing that in Linq to Sql returns ISingleResult<T> or IMultipleResults<T> kinds of value but i wasn't know how to get it in Integer. Thanks a lot.

  • wriiju,

    Wil this same approach work for bit values?

    Thanks

    Steve

    SteveStacel@gmail.com

Page 1 of 1 (10 items)