简体   繁体   English

使用LINQ从存储过程中获取值

[英]Using LINQ to get values from Stored Procedure

I am using LINQ and DataContext to retrieve my data. 我正在使用LINQ和DataContext检索我的数据。 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. 我已阅读以下文章: http : //www.codeproject.com/Articles/37938/Simple-6-steps-to-use-stored-procedure-in-LINQ ,其中指定了如何调用存储过程。 But one thing is that it seems that I should at least be able to define which table I am refering to (step 2) 但是有一件事是,看来我至少应该能够定义我要引用的表(步骤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: 首先,我创建一个名为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: 接下来,我创建一个存储过程,该过程可以对我的表进行填充并返回新插入值的ID:

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. 在我的应用程序中,我创建了一个.dbml文件,该文件“包含”我的存储过程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: 调用此过程时,它将从插入的值中返回Id,然后我可以创建一个简单的LINQ语句来获取该值:

'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. 如果需要,我什至可以通过再次删除TemporaryParking中的值来进行清理。

Thank you Thorsten Dittmar for pointing me in the right direction! 谢谢Thorsten Dittmar为我指出正确的方向! Your answer didn't do the whole trick though. 您的答案并不能解决所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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