批量插入 Dbase (.dbf) 文件的有效方法
我目前使用 OleDBCommand.ExecuteNonQuery (重复调用)从源 DataTable 一次将多达 350,000 行插入到数据库文件 (*.dbf) 中。我重用 OleDbCommand 对象和 OleDbParameters 来设置每次调用插入语句时要插入的值。目前插入 350,000 行需要我的程序大约 45 分钟。
有没有更有效的方法来做到这一点? Dbase (*.dbf) 文件是否存在类似于 SQL Server 中使用的批量插入选项的功能?
Im currently using OleDBCommand.ExecuteNonQuery (repeatedly called) to insert as much as 350,000 rows into dbase files (*.dbf) at a time from a source DataTable. I'm reusing an OleDbCommand object and OleDbParameters to set the values to be inserted each time when the insert statement is called. Inserting 350,000 rows currently takes my program about 45 mins.
Is there a more efficient way to do this? Does something similar to the Bulk Insert option used in SQL Server exist for Dbase (*.dbf) files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过将 OleDB 驱动程序从 Jet 更改为 vfpoledb 修复了该问题。这将总时间从 40 分钟减少到 8 分钟。
vfpoledb 驱动程序和合并模块是从以下链接下载的
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=e1a87d8f-2d58-491f-a0fa-95a3289c5fd4
感谢您的帮助。
Fixed the problem by changing the OleDB driver from Jet to vfpoledb. This cut the total time from 40 mins to 8 mins.
The vfpoledb driver and merge module was downloaded from the link below
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=e1a87d8f-2d58-491f-a0fa-95a3289c5fd4
Thanks for your help.
如果给定表的文件扩展名为 .dbf 和 .cdx,则它可能是 Visual FoxPro 表,而不是专门的“dBase”。
如果是这种情况,VFP 允许“附加自”命令,看起来像这样...
use (yourTable)
从 SomeFile.txt 类型 csv 追加
,但是也接受其他导入文件格式,如 XLS、DELIMITED 等。
如果是这样,VFP 还允许使用 ExecScript() 命令,您可以在其中构建表示要执行的命令的字符串,然后像普通 PRG 一样运行它们。 VFP 命令库中并非所有内容都可用,但足以满足您的需要。您需要使用 VFP OleDb 提供程序,按照您已经执行的操作进行连接。然后,构建一个类似的脚本...
然后,发出您的
如果传入源的结构与您预期的表不同,您还可以创建一个临时表(VFP 中的光标),其中的列按照它们匹配的顺序排列输入源,但与它们将被拉入的最终表具有相同的列名和数据类型,然后使用它作为基础追加到最终表中...VFP中的游标是自清理的..即:它们是临时的文件在关闭时立即删除,关闭您的连接将释放它们...例如
THEN,发出您的
编辑--来自反馈
由于它基于DBASE,因此我会考虑仍然下载VFP OleDb Manager,并与其建立连接与上面的内容相同,但不是在末尾使用表并附加到它,而是仅更改结尾部分...
删除
并放入 int
复制到 FOXPLUS 类型会将其放入磁盘上的物理表中,DBASE 将通过 IT 识别该表OleDb 提供者。然后,回到与数据库的连接,我会做一个
是的,它涉及一个新的 OleDb Provider,但是 VFP 在执行此类导入时表现出色,不费吹灰之力...
If its a .dbf and .cdx file extensions for a given table, its probably a Visual FoxPro table and not specifically "dBase".
If this is the case, VFP allows for an "append from" command and looks something like this...
use (yourTable)
append from SomeFile.txt type csv
however, other import file formats are accepted too like XLS, DELIMITED and others.
If so, VFP also allows for an ExecScript() command where you can build a string representing commands to be executed, then runs them as if a normal PRG. Not everything in the VFP command library is available, but plenty for what you need. You would need to be using the VFP OleDb provider, make a connection as you are already doing. Then, build a script something like...
THEN, issue your
If the structure of the incoming source is not the same as your expected table, you can also create a temporary table (cursor in VFP), with columns in the order they match the input source, but have the same column names and data types as the FINAL table they will be pulled into, then use that as basis to append into final table... Cursors in VFP are self-cleaning.. ie: they are temp files immediately erased when closed, and closing your connection will release them... such as
THEN, issue your
EDIT -- from feedback
Since it IS based on DBASE, I would then consider still downloading the VFP OleDb Manager, doing a connection to that with the above, but instead of using your table at the end and appending to it, change only the ending part...
remove
and put int
The copy to type FOXPLUS will put it into a physical table on disk that DBASE will recognize through ITs OleDb Provider. Then, back to a connection to your dbase, I would do an
Yes, it involves a new OleDb Provider, but VFP SCREAMS performance on doing such imports without breaking a sweat...
从您的其他反馈来看,作为 SQL Server,SQL Server 具有批量上传功能。
我将创建一个存储过程,该过程需要您尝试上传的文件的名称,并在那里完成所有操作。与我描述的使用 Foxpro 的方式类似,我将在 SQL 中创建一个与要导入的列匹配的临时表(如果需要更快地预填充数据),然后导入该表。一旦进入临时结构,您就可以对所需的数据进行任何清理。准备好后,然后将其作为临时导入表中的选择插入到主表中。
From your other feedback, being SQL Server, SQL Server has bulk upload capabilities.
I would create a stored procedure that expects the name of the file you are trying to upload and do it all there. In a similar fashion as I described doing with Foxpro, I would create a temporary table (if needed for faster pre-population of data) in SQL that matches the column about to be imported, then do import into that. Once in the temp structure, you can do whatever cleansing of the data you need. When ready, then insert into your primary table as a select from the temp import table.