Database Steps
Create One Table in Database
CREATE TABLE [dbo].[emp_detail](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[age] [int] NULL,
[location] [nchar](10) NULL
) ON [PRIMARY]
Create One User Define Table Type in Database
CREATE TYPE [dbo].[empdetail] AS TABLE(
[name] [varchar](100) NULL,
[age] [int] NULL,
[location] [varchar](100) NULL
)
Create One store Procedure in Database
ALTER PROCEDURE [dbo].[SP_INSERT_EMPLOYEE]
(
@emp_detail empdetail READONLY
)
AS
BEGIN
insert into emp_detail([name],[age],[location])
select [name],[age],[location] from @emp_detail;
END
C# Steps
Create Datatable
DataTable dtEmployeeDetail = new DataTable();
dtEmployeeDetail.Columns.Add("name");
dtEmployeeDetail.Columns.Add("age");
dtEmployeeDetail.Columns.Add("location");
for (int i=0;i<3;i++)
{
var dr = dtEmployeeDetail.NewRow();
dr["name"] = string.Concat("A",i.ToString());
dr["age"] = i;
dr["location"]= string.Concat("A", i.ToString());
dtEmployeeDetail.Rows.Add(dr);
}
Create Connection and open it
string conn = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlCommand cmdProc = new SqlCommand("SP_INSERT_EMPLOYEE", con);
cmdProc.CommandType = CommandType.StoredProcedure;
cmdProc.Parameters.AddWithValue("@emp_detail", dtEmployeeDetail);
cmdProc.ExecuteNonQuery();
con.Close();