简体   繁体   中英

Using LINQ to get values from Stored Procedure

I am using LINQ and DataContext to retrieve my data. I do not use the Entity Framework. And now I have a stored procedure which I do not know

  1. how to provide parameters and
  2. how to define and get the result from (single value).

The stored procedure is defined this way:

CREATE PROCEDURE [dbo].[spGetConvertedFieldValue] 
-- Add the parameters for the stored procedure here
    @Table NVARCHAR(50),
    @Field NVARCHAR(50),
    @Key NVARCHAR(50),
    @KeyValue NVARCHAR(500)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    DECLARE @SQL NVARCHAR(MAX)

    SET @SQL = 'SELECT ' + @Field + ' FROM ' + @Table + ' WHERE ' + @Key + ' = ''' + @KeyValue + ''''

EXEC(@SQL)
END

As you see this is a very generic stored procedure! Reason for doing it like this is, that I have another table where it is possible to enter the above values and the the system will be able to build my end result.

But I'm kind of stocked right here. I have read the article: http://www.codeproject.com/Articles/37938/Simple-6-steps-to-use-stored-procedure-in-LINQ where it is specified how to call a stored procedure. But one thing is that it seems that I should at least be able to define which table I am refering to (step 2)

And further, the above article does not describe how to add my arguments.

If it is possible to create the result by not using a stored procedure that would also be an option!

Okay this is the work-around:

First I create a new table called TemporaryParking:

CREATE TABLE [dbo].[TemporaryParking](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Field] [nvarchar](max) NULL,
 CONSTRAINT [PK_TemporaryParking] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  =     ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 10) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

Next I create a stored procedure, which can polulate my table AND return the id of the new, inserted value:

CREATE PROCEDURE [dbo].[spGetConvertedFieldValue] 
-- Add the parameters for the stored procedure here
@Table NVARCHAR(50),
@Field NVARCHAR(50),
@Key NVARCHAR(50),
@KeyValue NVARCHAR(500)
AS
BEGIN
SET NOCOUNT ON;

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 'INSERT INTO TemporaryParking (Field) SELECT ' + @Field + ' FROM ' + @Table + ' WHERE ' + @Key + ' = ''' + @KeyValue + ''''

EXEC(@SQL)

RETURN @@Identity   
END

In my application I have created a .dbml file which 'contains' my stored procedure spGetConvertedFieldValue.

When calling this procedure it will return the Id from the inserted value and then I can create a simple LINQ statement where I get the value:

'lets assume that I now have the Id in a variable calle tmpId

var rv = (from f in _db.TemporaryParking
         where f.Id == tmpId
         select f.Field).First();

And if I want to, I can even clean up by deleting the value from the TemporaryParking again.

Thank you Thorsten Dittmar for pointing me in the right direction! Your answer didn't do the whole trick though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM