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
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.
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.
You can try with SqlBulkCopy class. Lets you efficiently bulk load a SQL Server table with data from another source.
If using SQL Server, SqlBulkCopy.WriteToServer(DataTable)
Or also with SQL Server, you can write it to a .csv and use BULK INSERT
If using MySQL, you could write it to a .csv and use LOAD DATA INFILE
If using Oracle, you can use the array binding feature of ODP.NET
If SQLite:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With