hello guys
I am developing project in MVC5.
how to add multiple rows and save all the records with a single button (save records in the database.)
hello guys
I am developing project in MVC5.
how to add multiple rows and save all the records with a single button (save records in the database.)
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sachin SinghPosted Nov 8, 2021, 6:25 PM
If you are using ADO.NET
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
foreach (var entry in entries)
dt.Rows.Add(new string[] { entry.Id, entry.Name });
using (SqlBulkCopy bc = new SqlBulkCopy(connection))
{ // the following 3 lines might not be neccessary
bc.DestinationTableName = "Entries";
bc.ColumnMappings.Add("Id", "Id");
bc.ColumnMappings.Add("Name", "Name");
bc.WriteToServer(dt);
}
For EntityFramework
YourContext.Employees.AddRange(
yourEmployeeList //List
);
For Dapper
using (var conn = new SqlConnection(cs))
{
var affectedRows = await conn.ExecuteAsync("insert into tblEmployee values(@Name,@Salary)", yourEmployeeList);
}
//Note:- parameter Name must match to your Employee class properties name
Srinivasan RamamoorthiPosted Nov 8, 2021, 2:21 PM