访问“紧凑和修复”以编程方式
是否可以以某种方式(使用 ADOX、使用 OleDbConnection 等)以编程方式“压缩和修复”Access 数据库?
Is it possible to "compact and repair" an Access database programmatically somehow (using ADOX, using OleDbConnection etc.)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我没有足够的代表来回复之前的“答案”,但我想提供一些可能对其他人关于OP的问题有帮助的信息。
多年来我一直使用 JRO 方法从 VB.net 压缩/修复我的 Access 2000 数据库。每隔一段时间,我就会有一个客户端设法损坏数据库(通常是在通过网络连接到数据库时,并且遭受意外的网络中断)。 JRO(根据我的经验)工作正常,只要数据库没有损坏。我永远无法弄清楚为什么如果我使用 Access 应用程序来修复数据库,可以修复数据库,但是当使用我的应用程序(使用 JRO)时,压缩/修复总是会失败(数据库采用无法识别的格式)。
因此,在一小时前遇到这个线程后,我将对 DAO 的引用放入我的应用程序中,并尝试了它修复损坏的数据库的能力,因为我今天有一个客户损坏了他们的数据库(大约 8 年来第三次发生) )。你猜怎么着,当 JRO 失败时,DAO 能够修复数据库!
好的,这就是我使用 JRO 与 DAO 的经验。希望有帮助。这是一段使用
CompactDatabase
的示例代码 来自 DAO:I don't have enough rep to reply to a previous "answer", but I wanted to provide some info that might be helpful to someone else in regards to the OP's question.
I have been using the JRO method for years to compact/repair my Access 2000 databases from VB.net. Every once in a blue moon, I have a client that has managed to corrupt a database (usually when connected to the database over a network and they suffer an unexpected network interruption). JRO (in my experience) works fine, as long as the database IS NOT CORRUPTED. I never could figure out why the database COULD be repaired if I used the Access application to do it, but when using MY application (which uses JRO), the compact/repair would always fail (database is in an unrecognized format).
So, after coming across this thread just an hour ago, I dropped a reference to DAO into my app and tried out its ability to repair a corrupted database as I just today had a client corrupt their database (third time its happened in about 8 years). Guess what, DAO was able to repair the database when JRO failed!
OK, so that is my experience with JRO vs. DAO. Hope it helps. Here is a piece of sample code for using
CompactDatabase
from DAO:在c#.net中只有四行代码
首先使用一个库:
您想要使用以下代码压缩和修复
test.mdb
:因此
test.mdb
将是压缩并修复,并将创建一个新文件test1.mdb
。然后,您只需删除test.mdb
并将test1.mdb
重命名为test.mdb
即可。It is just four lines of code in c#.net
First use a library:
You want to compact and repair
test.mdb
with the following code:Thus
test.mdb
will be compacted and repaired and a new filetest1.mdb
will be created. Then you just have to deletetest.mdb
and renametest1.mdb
totest.mdb
.可以通过两种方式压缩和修复 MS ACCESS 数据库:
RepairDatabase()
,而在 DAO360 中,有一个CompactDatabase()
JRO:举个例子,在 VB6(旧、旧、旧...)中执行以下操作:
您会注意到,该函数要求您指定要修复的数据库的名称以及已修复数据库的名称。
it'possible compacting and repairing an MS ACCESS database in two ways:
RepairDatabase()
, while in DAO360 there'sCompactDatabase()
As an example, in VB6 (old, old, old...) do this:
As you will notice, the function requires that you specify the name of the db to be repaired and the name of the repaired database.
VBScript 的示例代码。
Sample code for VBScript.
此解决方案适用于 Access 2010 数据库引擎:
所需参考:
代码:
(sourceFilePath 和 destFilePath 不应相同!)
CompactDatabase 方法参数(来自反射):
确保在与 AccessDatabaseEngine(或 Office)相同的平台下运行它)您安装了(x86/x64)。
This solution works with the Access 2010 Database Engine:
Required reference:
Code:
(The sourceFilePath and destFilePath should not be the same!)
CompactDatabase method parameters (from reflection):
Make sure you run it under the same platform as the AccessDatabaseEngine (or Office) you installed (x86/x64).
这是微软官方链接,任何进一步的评论都是多余的。
DBEngine.CompactDatabase 方法
Here is the official MS link, any further comments would be redundant.
DBEngine.CompactDatabase Method