分享
 
 
 

利用ado压缩数据库(vc源代码)

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

HOWTO: Compacting Microsoft Access Database Through OLE DB

http://support.microsoft.com/default.aspx?scid=kb;EN-US;230496

SUMMARY

OLE DB specification doesn't 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 see knowledge 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 IDBCreateSession.

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 <objbase.h>

#define DBINITCONSTANTS

#define INITGUID

#include <initguid.h>

#include <oledb.h>

#include "msjetoledb.h"

#include "jetoledb.h" // for IJetCompact interface

#include <atldbcli.h>

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<IJetCompact> spJetCompact =NULL;

CComPtr<IDBCreateSession> spSession =NULL;

HRESULT hr=0;

//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 connected to the database?

ATLASSERT(ds.m_spInit != NULL);

hr = ds.m_spInit->QueryInterface(IID_IDBCreateSession, (void**)&spSession);

if (FAILED(hr))

return hr;

//IJetCompact 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 exists

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 our variant to VT_I4 and 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 );<BR/>

<BR/>

// 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 in the 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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有