OLEDB slow performance when inserting multiple rec

2019-08-02 04:54发布

问题:

I am using an OLEDB connection (Microsoft.ACE.OLEDB.12) to read records from a C# data table and put the records into an excel spreadsheet.

I am looping round each record in my data table and building up OLEDBCommand.CommandText using an "Insert into table name values (" etc. I am having to format the values appropriately so that string/char/integer/decimal are inserted correctly. This all works fine and my spreadsheet is created, however when there are loads of records to insert (e.g. 500,000 plus), then the performance is really slow and it takes for ever. Is there a quicker way of doing this, rather than reading one record from my C# datatable at a time, making sure that the SQL statement has the correct syntax for the datatype and inserting it one at a time?

Any help appreciated

Thanks

回答1:

Make sure that you use prepared statements for inserts http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.prepare.aspx