简体   繁体   English

从C#批量插入Access数据库?

[英]Bulk Insert into access database from c#?

How can I do this. 我怎样才能做到这一点。 I have about 10000 records in an an Excel file and I want to insert all records as fast as possible into an access database? 在Excel文件中有大约10000条记录并且想将所有记录尽快插入到Access数据库中?

Any suggestions? 有什么建议么?

What you can do is something like this: 您可以执行以下操作:

    Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Test Files\db1 XP.mdb") 
    AccessConn.Open() 
    Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [ReportFile] FROM [Text;DATABASE=C:\Documents and Settings\...\My Documents\My Database\Text].[ReportFile.txt]", AccessConn) 
    AccessCommand.ExecuteNonQuery() 
    AccessConn.Close() 

Switch off the indexing on the affected tables before starting the load and then rebuilding the indexes from scratch after the bulk load has finished. 开始加载之前,请关闭受影响表的索引,然后在大容量加载完成后从头开始重建索引。 Rebuilding the indexes from scratch is faster than trying to keep them up to date while loading large amount of data into a table. 从头开始重建索引比尝试在将大量数据加载到表中时保持最新状态要快。

If you choose to insert row by row, then maybe want to you consider using transactions. 如果选择逐行插入,则可能要考虑使用事务。 Like, open transaction, insert 1000 records, commit transaction. 像,打开事务,插入1000条记录,提交事务。 This should work fine. 这应该工作正常。

Use the default data import features in Access. 使用Access中的默认数据导入功能。 If that does not suit your needs and you want to use C#, use standard ADO.NET and simply write record-for-record. 如果那不适合您的需要,并且您想使用C#,请使用标准ADO.NET并简单地写记录。 10K records should not take too long. 10K记录不应花费太长时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM