How do I compact and repair an ACCESS 2007 databas

2019-05-07 19:42发布

问题:

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.

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.

This is driving me crazy.

回答1:

Seems like Office 12 Database Engine doesn't have any documentation anywhere.

Not quite true. There is limited information available.

On MSDN, see:

Access 2007 Developer Reference

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.

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.

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.



回答2:

I know this is an old thread, but this worked for me (for VB.Net). Hopefully it can help someone down the road:

​Add a reference to "Microsoft Office 12.0 Access database engine Object Library"

Dim dbe As New Microsoft.Office.Interop.Access.Dao.DBEngine

dbe.CompactDatabase("C:\folder\database.accdb",  "C:\folder\database_New.accdb", , , ";pwd=<database password>")

http://www.tolchin.net/KB/Lists/Posts/Post.aspx?ID=9



回答3:

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.

BTW, an Engine Type = 5 is Jet 4.0, so that should be a dead giveaway as to version created.



回答4:

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


回答5:

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.

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).

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.