Skip to content
Loading
how to store the data table content to sql data base
  • Harshal Patil
    if your DataTable coloumn and Sql table coloumn are same then you can use SqlBulkCopy
     
     
    or 
    for one by one row you can use
     
    Foreach (DataRow dr in Dt.rows)
    {
     insert into table values(dr[0].toString(),dr[1].toString());
     
    }
  • Prateek Singla
    Please check these Links:-
     
    http://www.c-sharpcorner.com/UploadFile/99bb20/import-excel-data-to-sql-server-in-Asp-Net/
     
    http://www.c-sharpcorner.com/UploadFile/0c1bb2/inserting-excel-file-records-into-sql-server-database-using/
     
    You can directly upload excel file to sql database just add oledb dll for it 
  • Salman Beg
    Hi.
    You can do like this,
      
    Create a User-Defined TableType in your database:

    CREATE TYPE [dbo].[MyTableType] AS TABLE(
    [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:

    using (var command = new SqlCommand("InsertTable") {CommandType = CommandType.StoredProcedure})
    {
    var dt = new DataTable(); //create your own data table
    command.Parameters.Add(new SqlParameter("@myTableType", dt));
    SqlHelper.Exec(command);
    }
    thanks.