简体   繁体   中英

Passing a list in place of sql parameter to a stored procedure

Is there a way to pass a list object as a parameter of a stored procedure?

for eg,

List<class1> Allobj = new List<class1>();
// assume Allobj .Count=5,(5 class1 objects)

now calling a stored procedure as

intResult = _helper.ExecuteNonQuery(base.Transaction, CommandType.StoredProcedure, "usp_InsertpurchaseEnquirydetails", Allobj );

or is there any other way to pass a list( atleast adding to a multidimensional sqlparameter array?

If possible how to pass and also how to iterate it from the stored procedure?

You're looking for TableValueParameters . Starting from SqlServer 2008 this feature is added. It will help you to pass a DataTable as a parameter.

Check out Table-Valued Parameters in msdn

You could use Table value parameter to pass a table or list to SQL stored procedures

Steps in brief:

1) Create a table value parameter in SQL server management studio

2) Define a parameter as created table value parameter (it should be readonly)

3) Set the C# sp parameter type to System.Data.SqlDbType.Structured

4) Pass your list as a CLR DataTable to stored procedure

The bellow link is a complete code example:

http://www.codeproject.com/Articles/39161/C-and-Table-Value-Parameters

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