gjijo
gjijo

Reputation: 1206

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?

Upvotes: 0

Views: 1092

Answers (2)

babak
babak

Reputation: 155

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

Upvotes: 1

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

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

Upvotes: 1

Related Questions