Create a
User-Defined TableType
in your database:CREATE TYPE [dbo].[MyTableType] AS TABLE(
[Id] int NOT NULL,
[Name] [nvarchar](128) NULL
)
Create a Table
in your database:
CREATE TABLE[dbo].[Records]( [Id] int NOT NULL,
[Name] [nvarchar](128) NULL
)
and define a parameter in your
Stored Ptocedure
:CREATE PROCEDURE [dbo].[InsertTable]
@myTableType MyTableType readonly
AS
BEGIN
insert into [dbo].Records
select * from @myTableType
END
and send your
DataTable
directly to sql server:protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(getConString()); using (var command = new SqlCommand("InsertTable") { CommandType = CommandType.StoredProcedure }) { var dt = GetDataTable(); //create your own data table command.Parameters.Add(new SqlParameter("@myTableType", dt)); command.Connection = con; con.Open(); command.ExecuteNonQuery(); //SqlHelper.Exec(command); } } private DataTable GetDataTable() { DataTable dt = new DataTable(); dt.Columns.Add("Id"); dt.Columns.Add("Name"); dt.Rows.Add("1","Dharmendra"); dt.Rows.Add("2", "Rakesh" ); dt.Rows.Add("3", "Mukesh" ); return dt; }
No comments:
Post a Comment