In most of the cases, we need to insert a large number of records into one or more tables in a SQL Server database.
SqlCommand
along with an INSERT INTO
statement at a time is very costly and slow.SqlBulkCopy
which is part of the System.Data.SqlClient
namespace.The SqlBulkCopy
class helps us transfer data from one data source to another, whether on a single server or between servers.
INSERT
statements, but SqlBulkCopy offers a significant performance advantage over them.SQLBulkCopy.WriteToServer()
method.using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
connection.Open();
using (var sqlBulk = new SqlBulkCopy(connection))
{
sqlBulk.DestinationTableName = "Customers";
sqlBulk.WriteToServer(dt);
}
}