简体   繁体   中英

passing parameter of list of values and tables to stored procedure

I'm in the point to implement an C# application that needs to consume already existing stored procedure that receive IDs or values in params. My task in charge is in two steps:

1- migrate stored procedure in order to receive list of IDs(int) and list of current params, means like a table

2- implement the layer that cal this procedures and will receive List and KeyValuePair or KeyValuePair

What should be the best approach to do this ?

EntityFramework to wrap SPs or not ORM at alla? How to implement List and KeyValuePair params ob SP side ? with Table-Valued params ?

I'm with SQL 2012

thanks,

Try in sql side User defined table type functionality and pass table as parameter in stored procedure.

For example:

CREATE TABLE Test
(
Id int NOT NULL IDENTITY (1, 1),
TestName varchar(50) NOT NULL,
Value int NULL
) ON [PRIMARY]

-- Create a table data type

CREATE TYPE [dbo].[TestType] As Table
(
--This type has structure similar to the DB table 
TestName varchar(50) NOT NULL,
Value int NULL
)

--This is the Stored Procedure

CREATE PROCEDURE [dbo].[TestProcedure]
(
    @Test As [dbo].[TestType] Readonly
)
AS
Begin
Insert Into Test(TestName,Value)
Select TestName, Value From @Test
End

C# code passing the data as follows:

DataTable dataTable = new DataTable("SampleDataType");

// We create column names as per the type in DB 

dataTable.Columns.Add("TestName", typeof(string)); 
dataTable.Columns.Add("Value", typeof(Int32)); 

// And fill in some values 

dataTable.Rows.Add("Metal", 99); 
dataTable.Rows.Add("HG", null);
...
SqlParameter parameter = new SqlParameter(); 

// The parameter for the SP must be of SqlDbType.Structured 

parameter.ParameterName="@Test"; 
parameter.SqlDbType = System.Data.SqlDbType.Structured; 
parameter.Value = dataTable; 

command.Parameters.Add(parameter); 

I dealt with this same issue just recently. The links in the comments above lay out how to do SPs with table valued parameters. I've used the TVP method and it was easy and clean.

When it comes to Entity Framework, you can make EF aware of the Stored Procedures and call them and get the results back into EF Objects. Here's a link:

http://msdn.microsoft.com/en-us/data/gg699321.aspx

It's quite a bit more work than just calling the SPs with ADO. A major consideration is whether the results returned by the SP map directly onto one of your objects. Suppose you're joining a couple of tables in a search. You'd have to make a new model for those results and map the SP to that model and for what? So everything will run slower.

If you're just reading data and the results don't map exactly to an existing model you should skip EF and use ADO directly. If, OTOH, you're doing reads and writes and you really want to keep everything in EF for consistency's sake, it is possible.

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