LINQ Query : How to return a single value from a stored procedure

P

psycho

How do we return a single value from a stored procedure.

Suppose I have a stored procedure like this:

create proc dbo.spInsertGroup
@ID uniqueidentifier
@GroupName varchar(100),
@IsActive bit
AS
BEGIN

SET @ID = newid()

insert into tblGroup
values(@ID, @GroupName, @IsActive)

select @ID

END

i am trying to return the same value using LINQ to SQL using stored
procedure.
But all the methods are failing as those types do not have a default
constructor

Is there any method by which i can perform this task.
 
B

bruce barker

LINQ does not support stored procedure. the LINQ to SQL data designer
supports adding stored procedure calls as methods to a data context. the
return value will be a collection of values. you will need to write your own
method if you just want a single return value.

-- bruce (sqlwork.com)
 
Joined
Nov 20, 2008
Messages
1
Reaction score
0
On the contrary, it is possible. You can do this:

1. in SQL procedure set ID as output parameter

create proc dbo.spInsertGroup
@ID uniqueidentifier OUTPUT
@GroupName varchar(100),
@IsActive bit
AS
BEGIN

SET @ID = newid()

insert into tblGroup
values(@ID, @GroupName, @IsActive)

select @ID
END


2. in your C# code:

DataClassesDataContext db = new DataClassesDataContext();
string ID = "";

var result = db.spInsertGroup(ref ID,"GroupName",true);


ID is then returned in string ID
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top