What is the best way to perform bulk inserts into an MS Access database from .NET? Using ADO.NET, it is taking way over an hour to write out a large dataset.
Note that my original post, before I "refactored" it, had both the question and answer in the question part. I took Igor Turman's suggestion and re-wrote it in two parts - the question above and followed by my answer.
Thanks Marc, in order to vote you I created an account on StackOverFlow...
Below is the reusable method [Tested on C# with 64 Bit - Win 7, Windows 2008 R2, Vista, XP platforms]
Performance Details: Exports 120,000 Rows in 4 seconds.
Copy the below code and pass the parameters... and see the performance.
The code:
Another method to consider, involving linking tables via DAO or ADOX then executing statements like this:
Please see my full answer here:
MS Access Batch Update via ADO.Net and COM Interoperability
First make sure that the access table columns have the same column names and similar types. Then you can use this function which I believe is very fast and elegant.
Thanks Marc for the examples.
On my system the performance of DAO is not as good as suggested here:
TestADONET_Insert_TransferToAccess(): 68 seconds
TestDAOTransferToAccess(): 29 seconds
Since on my system the use of Office interop libraries is not an option I tried a new method involving the writing of a CSV file and then importing it via ADO:
Performace analysis of TestADONET_Insert_FromCsv(): 1.9 seconds
Similar to Marc's example TestTextTransferToAccess(), this method is also fragile for a number of reasons regarding the use of CSV files.
Hope this helps.
Lorenzo
To add to Marc's answer:
Note that having the
[STAThread]
attribute above your Main method. will make your program easily able to communicate with COM objects, increasing the speed further. I know it's not for every application but if you heavily depend on DAO, I would recommend it.Further more, using the DAO insertion method. If you have a column that is not required and you want to insert null, don't even set it's value. Setting the value cost time even if it's null.
You can use a KORM, object relation mapper that allows bulk operations over MsAccess.
or if you have source reader, you can directly use
MsAccessBulkInsert
class:KORM is available from nuget Kros.KORM.MsAccess and it's opensource on GitHub