I need to compact and repair an Access 2007 .accdb database file. I know that JRO.JetEngine can do this with .mdb files, but I need to repair the newer version 2007 format by code.
我需要压缩和修复Access 2007 .accdb数据库文件。我知道JRO.JetEngine可以使用.mdb文件执行此操作,但我需要通过代码修复较新的版本2007格式。
Any suggestions?
有什么建议么?
EDIT:
编辑:
Here is the thing: I found that I can use the COM object library "Microsoft Office 12 Access Database Engine Object Library" and use the DBEngine class and call its CompactDatabse method. But there doesn't seem to be a place for me to provide the database password; Seems like Office 12 Database Engine doesn't have any documentation anywhere. I found some documentation for older versions of CompactDatabase method but those don't help me at all.
事情就是这样:我发现我可以使用COM对象库“Microsoft Office 12 Access数据库引擎对象库”并使用DBEngine类并调用其CompactDatabse方法。但似乎没有地方可以提供数据库密码;似乎Office 12数据库引擎在任何地方都没有任何文档。我找到了旧版本的CompactDatabase方法的一些文档,但这些文档根本没有帮助我。
This is driving me crazy.
这真让我抓狂。
6
Seems like Office 12 Database Engine doesn't have any documentation anywhere.
似乎Office 12数据库引擎在任何地方都没有任何文档。
Not quite true. There is limited information available.
不完全正确。信息有限。
On MSDN, see:
在MSDN上,请参阅:
Access 2007 Developer Reference
Access 2007开发人员参考
There is a branch for Microsoft Jet Replication Objects (JRO) Reference, so JRO (one of the libraries comprising ADO classic) is still officially supported (and AFAIK does still work) for ACE e.g. the accdb format. The CompactDatabase method takes two parameters, both of which is an OLE DB connection string. Therefore, supplying the database password should be no different from a regular connection e.g. using Jet OLEDB:Database Password
in the connection string. Remember, for accdb format you need to include Jet OLEDB:Engine Type=5
in the connection string e.g.
有一个用于Microsoft Jet复制对象(JRO)参考的分支,因此对于ACE,JRO(包含ADO classic的库之一)仍然是官方支持(并且AFAIK仍然可以工作)。 accdb格式。 CompactDatabase方法有两个参数,两个参数都是OLE DB连接字符串。因此,提供数据库密码应该与常规连接没有区别,例如使用Jet OLEDB:连接字符串中的数据库密码。请记住,对于accdb格式,您需要在连接字符串中包含Jet OLEDB:Engine Type = 5,例如
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\MyDB.accdb;
Jet OLEDB:Database Password=MYPASSWORD;
Jet OLEDB:Engine Type=5
For what it's worth (not much, as it turns out), limited documentation for the ACE engine is found in the Office Help for Access 2007.
对于它的价值(事实证明并不多),ACE引擎的有限文档可以在Access 2007 for Access 2007中找到。
Bear in mind, most of the Access Database Engine was developed during the 1990s when proprietary formats were all the rage; I suspect documentation was suppressed for commercial reasons and the knowledge is simply lost. So there are many holes in the currently-available documentation, and large ones at that. Often, the only way of finding out how the engine works is to discover it through actual usage. The documentation that does exist contains some appalling errors: already today I've posted a couple of examples on SO where potentially use yet fictitious functionality (LIMIT TO nn ROWS, CREATE TEMPORARY TABLE, etc) has been advertised in the Access Help. Caveat emptor.
请记住,大多数Access数据库引擎都是在20世纪90年代开发的,当时专有格式风靡一时;我怀疑文档是出于商业原因而被压制而且知识遗失了。因此,目前可用的文档中存在许多漏洞,而且存在大量漏洞。通常,找出引擎如何工作的唯一方法是通过实际使用来发现它。存在的文档包含一些令人震惊的错误:今天我已经在SO上发布了几个示例,其中可能使用但虚构的功能(LIMIT to nn ROWS,CREATE TEMPORARY TABLE等)已在Access帮助中公布。买者自负。
3
I know this is an old thread, but this worked for me (for VB.Net). Hopefully it can help someone down the road:
我知道这是一个旧线程,但这对我有用(对于VB.Net)。希望它可以帮助有人在路上:
Add a reference to "Microsoft Office 12.0 Access database engine Object Library"
添加对“Microsoft Office 12.0 Access数据库引擎对象库”的引用
Dim dbe As New Microsoft.Office.Interop.Access.Dao.DBEngine
dbe.CompactDatabase("C:\folder\database.accdb", "C:\folder\database_New.accdb", , , ";pwd=")
http://www.tolchin.net/KB/Lists/Posts/Post.aspx?ID=9
http://www.tolchin.net/KB/Lists/Posts/Post.aspx?ID=9
2
Just an FYI regarding JRO, it does not support Access version 2007 or higher database files. While the CompactDatabase method in this library appears to function when using the ACE OLEDB Provider, it will in fact generate a 2002-2003 (Jet 4.0) database file.
仅仅是关于JRO的FYI,它不支持Access版本2007或更高版本的数据库文件。虽然此库中的CompactDatabase方法在使用ACE OLEDB Provider时似乎起作用,但它实际上会生成2002-2003(Jet 4.0)数据库文件。
BTW, an Engine Type = 5 is Jet 4.0, so that should be a dead giveaway as to version created.
BTW,引擎类型= 5是Jet 4.0,因此对于版本创建应该是一个死的赠品。
2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Data;
using Microsoft.Office.Interop.Access.Dao;
protected void btnSubmitDenim_Click(object sender, EventArgs e)
{
try
{
string oldFileName = HttpContext.Current.Server.MapPath(@"~/App_Data/MyDatainAccess2010.accdb");
string newFileName = HttpContext.Current.Server.MapPath(@"~/App_Data/Temp.accdb");
// Obtain a reference to the Access 2010 introp DBEngine formally known as JetEngine
DBEngine engine = (DBEngine)HttpContext.Current.Server.CreateObject("Dao.DBEngine.120");
// Compact the database (saves the compacted version to newFileName)
engine.CompactDatabase(oldFileName, newFileName);
// Delete the original database
File.Delete(oldFileName);
// Move (rename) the temporary compacted database to
// the original filename
File.Move(newFileName, oldFileName);
// The operation was successful
lblResult.Text = "Database Compact completed";
}
catch
{
// We encountered an error
lblResult.Text = "Process Failed";
}
}
// Hope that helps
// Wasif
1
You don't need JRO. JRO should not even exist -- it is one of the ugly stepchildren of ADODB that came about because of Microsoft's misguided attempt to replace Access/Jet/ACE's native DAO with ADO, a non-native abstraction layer. JRO is there to provide support for Jet-specific functionality not available in ADODB.
你不需要JRO。 JRO甚至不应该存在 - 它是ADODB的丑陋继子之一,因为微软试图用ADO(非本地抽象层)取代Access / Jet / ACE的原生DAO。 JRO可以为ADODB中没有的Jet特定功能提供支持。
If you used DAO instead, you'd have all the functionality right there already, and it works for all formats supported by the version of Access you're using (since the DAO version is synchronized with your Access version, that is, when the db engine is enhanced, there's a new version of DAO to go with it).
如果您使用DAO,那么您已经具备了所有功能,它适用于您正在使用的Access版本所支持的所有格式(因为DAO版本与您的Access版本同步,也就是说,数据库引擎得到了增强,还有一个新版本的DAO可以使用它。
So, in A2007, you'd simply use the DAO compact methods (there's been no repair operation as a separate command since Access 2, and a repair happens only if the database engine determines during the compact that a repair is necessary). If you're working from outside Access, then you need to use the ACE-compatible version of DAO. If you do not have A2007 installed and have installed the ACE independently, you have that version of DAO installed along with it.
因此,在A2007中,您只需使用DAO紧凑方法(自Access 2以来,没有任何修复操作作为单独的命令,并且只有在数据库引擎在压缩期间确定需要修复时才会进行修复)。如果您在Access外部工作,则需要使用兼容ACE的DAO版本。如果您没有安装A2007并且已经独立安装了ACE,那么您将安装该版本的DAO。