Introduction
SQL Server Stored Procedures support System.Data.DataTable as a parameter. We can pass the DataTable to the Stored Procedure using ADO.Net in the same way as we provided using the System.Data.SqlParameter class, but needs a few changes in the datatype.
Normally we provide DbType of SqlParameter for a normal parameter like varchar, nvarchar, int and so on as in the following code.
- SqlParameter sqlParam= new SqlParameter();
- sqlParam.ParameterName = "@StudentName";
- sqlParam.DbType = DbType.String;
- sqlParam.Value = StudentName;
But in the case of a Table parameter, we do not need to provide a
DbType as the parameter data type. We need to provide
SqlType rather then
DbType.
Example
- SqlParameter Parameter = new SqlParameter;
- Parameter.ParameterName = "@PhoneBook";
- Parameter.SqlDbType = SqlDbType.Structured;
- Parameter.Value = PhoneTable;
The following example receives a list of phone books and stores them in a database using ADO.Net. The example retrieves the phone book details from the list and stores them into the DataTable and passes this table to the Stored Procedure named NewPhoneBook as a parameter.
-
-
- List<PhoneBook> PhoneBooks
-
- DataTable PhoneTable = new DataTable();
-
-
- DataColumn COLUMN=new DataColumn();
- COLUMN.ColumnName="ID";
- COLUMN.DataType= typeof(int);
- PhoneTable.Columns.Add(COLUMN);
-
- COLUMN = new DataColumn();
- COLUMN.ColumnName = "ContactNumber";
- COLUMN.DataType = typeof(string);
- PhoneTable.Columns.Add(COLUMN);
-
- COLUMN = new DataColumn();
- COLUMN.ColumnName = "ContactName";
- COLUMN.DataType = typeof(string);
- PhoneTable.Columns.Add(COLUMN);
-
-
- foreach (UserPhoneBook UPB in PhoneBooks)
- {
- DataRow DR = PhoneTable.NewRow();
- DR[0] = UPB.UserName;
- DR[1] = UPB.ContactNumber;
- DR[2] = UPB.ContactName;
- PhoneTable.Rows.Add(DR);
- }
-
- SqlParameter[] Parameter = new SqlParameter[2];
- Parameter[0].ParameterName = "@PhoneBook";
- Parameter[0].SqlDbType = SqlDbType.Structured;
- Parameter[0].Value = PhoneTable;
-
- Parameter[1].ParameterName = "@Return_Value";
- Parameter[1].Direction = ParameterDirection.ReturnValue;
-
- SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "[NewPhoneBook]", Parameter);
Summary
I hope you have learned how to pass a table to a Stored Procedure as a parameter using ADO.Net in C#.
ratnesh kumarPosted Apr 6, 2020, 11:52 PM
Useful knowledge.
usha lekkalaPosted Feb 12, 2020, 5:55 AM
While clicking datagridview cell I want to call stored procedure and generate excel . can you help me.(c#)
usha lekkalaPosted Feb 12, 2020, 5:53 AM
Hi kailash can you help me with ...
NitinPosted Apr 5, 2015, 9:36 AM
Nice
Deepak VermaPosted Mar 30, 2015, 6:50 AM
Hi Kailash Chandra Behera, It'll be good if you tell, what will be the datatype of parameter '@PhoneBook' in MS Sql Server Stored Procedure and which versions are supported. Please provide any back-end sample.
Syed ShanuPosted Mar 29, 2015, 9:10 PM
Good Work.Really a good and useful one for members.If its poosible for you to update with a sample source file with example, it will be more useful for the members.
Rahul Kumar SaxenaPosted Mar 29, 2015, 1:02 PM
Good work
Khargesh RajputPosted Mar 29, 2015, 3:11 AM
nice sir
RakeshPosted Mar 28, 2015, 8:14 PM
Nice one