HOWTO: Compacting Microsoft Access Database THROUGH OLE DB
http://support.microsoft.com/default.aspx?scid=kb;n-us;230496 summary
. OLE DB specification does not provide interfaces to compact or repair databases However, the OLE DB Provider for Microsoft Jet version 4.0 exposes this functionality through a custom interface:. IJetCompact (TDataSource cotype) IJetCompact is defined in the header file Jetoledb.h that Can Be Obtained Separately from Microsoft (please seenowledge base article q228525).
More information
TO Repair and Compact A Microsoft Access Database Using Ole DB, MDAC 2.1 or Higher Version Must Be Properly Installed on The Computer. The Following Steps Are Required:
1. Create and initialize the data source objects by using interfaces of idbinitialize and idbcreatesis.
2. Use getjetenginetype function to get the version of jet database.
3. Fill A DBPropset Structure with Information About The Destination Database to Compact TO.
4. Query The IDbcreateSession Object for the Jet Compact Object: iJetCompact.
5. Call iJetCompact's Compact Method Passing in The DBPropset Created in Step 2.
The Following Code Demonstrates The Earlier Steps Using Visual C ATL OLE DB Consumer Templates:
#include
#define dbinitconstants # define initguid
#include
Long getjetenginetype (lpctstr src);
Class oleinitialize {bool m_boleinit; public: oleinitialize () {m_boleinit = (Coinitialize (null) == S_OK);
} ~ Oleinitialize () {if (m_boleinit) Couninitialize ();}}; HRESULT CompactDatabase (LPCTSTR SRC, LPCTSTR DEST) {// Initialize Environment Must Be The First Line in your function Oleinitialize Oleinit;
CDataSource DS; ccomptr
// specify the source dso ds.open (CLSID_JETOLEDB_4_00, SRC);
CdbPropset PropSet1 (dbpropset_dbinit); Propset1.addProperty (dbprop_init_datasource, dest);
Long x = getjetenginetype (src); cdbpropset propset2 (dbpropset_jetoledb_dbinit); PropSet2.addProperty (dbprop_jetoledb_engine, x);
CDBPropset dbsets [2] = {PropSet1, PropSet2};
// have we connect to the database? Atlassert (ds.m_spinit! = Null); hr = ds.m_spinit-> queryinterface (IID_IDBCREATESSION, (VOID **) & spsession); if (Failed (HR)) Return HR;
// i i i omPact Only Supported in Jet 4.0 and Above HR = spsession-> queryinterface (_UUIDOF (iJetCompact), (void **) & spjetcompact; if (Failed (HR)) Return HR;
// delete the destination file if it exissrs remove (dest);
// ok compact // hr = spjetcompact-> Compact (1, & propset); hr = spjetcompact-> Compact (1, dbsets);
IF (FAILED (HR)) Return Hr; Return HR;
}
LONG GETJETENGINETYPE (LPCTSTR SRC) {HRESULT HR; CDataSource DS; CCOMBSTR BSTRSOURCE; VARIANT VPROPVALUE
// Initialize out 0. vpropvalue.vt = vt_i4; vpropvalue.lval =
0L
;
// Exit now if source database is null. If (null == src) Return 0;
// Build connection string for source bstrSource = L "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =";. BstrSource = src; bstrSource = L ";"; hr = ds.OpenFromInitializationString (bstrSource); hr = ds.GetProperty (DBPROPSET_JETOLEDB_DBINIT, DBPROP_JETOLEDB_ENGINE, & vPropValue);
// Version returned will be one of these values: // // #define JETDBENGINETYPE_UNKNOWN 0x00 // #define JETDBENGINETYPE_JET10 0x01 // #define JETDBENGINETYPE_JET11 0x02 // #define jetdbenginetype_jet2x 0x03 // #define jetdbEngineTyPE_JET3X 0x04 // #define jetdbenginetype_jet4x 0x05
Return vpropvalue.lval;
}
Note:
1. if you have a compiler error Such as:
Dbprop_jetoledb_engine undefined Specifier
For Additional Information About Updated Jet Header Files, please click The article Number Below to View The Article In The Microsoft Knowledge Base:
228525 Patch: Jetvc.exe VC Support Files for the Jet OLE DB Provider
2. Inline the getjetenginetype function in your code if you don't want to open another connection.
3. Compacting a Database Also Repairs The Database, Unlike in Dao WHERE IT WAS A Separate FunctionAlity. In Ole DB There IS No Way To Only Repair A Database.
References
For additional information, please click the article Number Below to view the article: MICROSOFT KNOWLEDGE BASE:
230501 HOWTO: Compacting Microsoft Access Database Via ADO
The Information in this Article Applies TO:
Microsoft OLE DB Provider for Jet 4.0
Microsoft Data Access Components 2.1
Microsoft Data Access Components 2.5
Microsoft Data Access Components 2.6
(www.sinoprise.com)