Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an array/table to stored procedure through Entity Framework

I am using Entity Framework with C# to make a Silverlight application. I have written some stored procedures which perform database intensive operations and I need to call them from Entity Framework after passing some parameters. In one of the steps, the users select a list of items for which they would need more details. This list of items (in the form of an array of integer IDs) need to be passed to the stored procedure to retrieve more information about those IDs. How do I pass this parameter to the stored procedure?

like image 416
Anand Avatar asked Jul 28 '11 14:07

Anand


People also ask

Can you pass a table to a stored procedure?

Table-Valued Parameters aka TVPs are commonly used to pass a table as a parameter into stored procedures or functions. They are helpful in a way, we can use a table as an input to these routines and we can get rid of dealing more complex steps to achieve this process.

Can we use Entity Framework with stored procedure?

Entity Framework has the ability to automatically build native commands for the database based on your LINQ-to-Entities or Entity SQL queries, as well as build the commands for inserting, updating, and deleting data. You may want to override these steps and use your own predefined stored procedures.

How do I pass a list as parameter in SQL stored procedure?

CREATE FUNCTION dbo. SplitInts ( @List VARCHAR(MAX), @Delimiter VARCHAR(255) ) RETURNS TABLE AS RETURN ( SELECT Item = CONVERT(INT, Item) FROM ( SELECT Item = x.i.value('(./text())[1]', 'varchar(max)') FROM ( SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(@List, @Delimiter, '</i><i>') + '</i>'). query('.

Which is faster EF or stored procedure?

I executed the same application at least 10 times and every time, the time taken by Entity Framework is almost 3-4 times more than the time taken by a stored procedure.


2 Answers

You can't pass table-valued parameters to SQL with the Entity Framework.

What you can do is create a delimited string like "1|2|3|4" and create a Split function in SQL that will return a table.

CREATE FUNCTION dbo.Split
(
    @RowData nvarchar(2000),
@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
Id int identity(1,1),
Data nvarchar(100)
) 
AS  
BEGIN 
Declare @Cnt int
Set @Cnt = 1

While (Charindex(@SplitOn,@RowData)>0)
Begin
    Insert Into @RtnValue (data)
    Select 
        Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

    Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
    Set @Cnt = @Cnt + 1
End

Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))

Return
END

Then if you need to do something like select all items from a table based on what is in the delimited string passed to your proc:

SELECT * FROM SomeTable WHERE Id IN (SELECT Id FROM dbo.Split(@DelStr, '|'))
like image 66
Dismissile Avatar answered Sep 28 '22 02:09

Dismissile


If you are using SQL Server, which I assume is the case, you can create use a table valued parameter to do what you wish. Using a table valued parameter prevents you from parsing an input parameter within the stored procedure and removes the threat of a SQL injection attack by eliminating the use of dynamic SQL.

Here is a great blog article that covers how to do what you wish to do.

Using Table-Valued Parameters in SQL Server 2008 and C#

like image 38
Dan Waterbly Avatar answered Sep 28 '22 02:09

Dan Waterbly