Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to Bulk Insert from a C# DataTable

Tags:

c#

.net-2.0

I have a DataTable that I want to push to the DB. I want to be able to say like

myDataTable.update(); 

But after reading the MSDN docs, apparently this does inserts row by row.

It should be noted that these statements are not performed as a batch process; each row is updated individually.

What are my alternatives?

Edit: I am using SQL Server 2005

like image 532
Joda Maki Avatar asked Feb 16 '11 21:02

Joda Maki


People also ask

How can I speed up bulk insert?

Below are some good ways to improve BULK INSERT operations : Using TABLOCK as query hint. Dropping Indexes during Bulk Load operation and then once it is completed then recreating them. Changing the Recovery model of database to be BULK_LOGGED during the load operation.

Is bulk insert faster than insert?

In case of BULK INSERT, only extent allocations are logged instead of the actual data being inserted. This will provide much better performance than INSERT. The actual advantage, is to reduce the amount of data being logged in the transaction log.

What is the best and fast way to insert 2 million rows of data into SQL Server?

You can try with SqlBulkCopy class. Lets you efficiently bulk load a SQL Server table with data from another source.


2 Answers

If using SQL Server, SqlBulkCopy.WriteToServer(DataTable)

  • SqlBulkCopy.WriteToServer Method (DataTable)

Or also with SQL Server, you can write it to a .csv and use BULK INSERT

  • BULK INSERT (Transact-SQL)

If using MySQL, you could write it to a .csv and use LOAD DATA INFILE

  • LOAD DATA INFILE Syntax

If using Oracle, you can use the array binding feature of ODP.NET

  • Bulk Insert to Oracle using .NET

If SQLite:

  • How do I bulk insert with SQLite?
  • Faster bulk inserts in sqlite3?
like image 171
JohnB Avatar answered Sep 19 '22 06:09

JohnB


string connectionString= ServerName + DatabaseName + SecurityType; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) {     connection.Open();     bulkCopy.DestinationTableName = "TableName";     try {         bulkCopy.WriteToServer(dataTableName);     } catch (Exception e) {         Console.Write(e.Message);     } } 

Please note that the structure of the database table and the table name should be the same or it will throw an exception.

like image 36
Vishal Kotak Avatar answered Sep 19 '22 06:09

Vishal Kotak