#import "../bin/msado15.dll" no_namespace rename ("EOF", "adoEOF")
#import "../bin/msjro.dll" no_namespace
// 压缩Access数据库
static bool compressMDB(const std::string& strFileName, const std::string& strNewFileName)
{
CoInitialize(NULL);
try
{
string strSourceConnection;
strSourceConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strSourceConnection += strFileName;
strSourceConnection += ";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Password='";
strSourceConnection += MDBPasswd; // 定义在ResourceDefines.h中
strSourceConnection += "'";
string strDestConnection;
strDestConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strDestConnection += strNewFileName;
strDestConnection += ";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Password='";
strDestConnection += MDBPasswd; // 定义在ResourceDefines.h中
strDestConnection += "'";
IJetEnginePtr jet(__uuidof(JetEngine));
jet->CompactDatabase(strSourceConnection.c_str(), strDestConnection.c_str());
}
catch(...)
{
CoUninitialize();
return false;
}
CoUninitialize();
return true;
}
// 打开Access数据库
static bool getConn(const string& fileWithFullPath, _ConnectionPtr& pConnection)
{
CoInitialize(NULL);
pConnection.CreateInstance(__uuidof(Connection));
string file;
file = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=/"";
file += fileWithFullPath + "/"";
file += ";Jet OLEDB:Database Password='";
file += MDBPasswd;
file += "'";
try
{
HRESULT hr;
hr = pConnection->Open(file.c_str(),"","",adModeUnknown);
if(hr == S_OK)
{
return true;
}
}
catch(_com_error e)
{
return false;
}
catch (...)
{
return false;
}
return false;
}
// 关闭Access数据库
static bool closeConn(_ConnectionPtr& pConnection)
{
try
{
if (pConnection->State)
pConnection->Close();
pConnection = NULL;
}
catch (...)
{
return false;
}
CoUninitialize();
return true;
}
// 打开Access数据库的ADO数据集
static bool getRecordSet(_ConnectionPtr& pConnection, const string& sql, _RecordsetPtr& pRecord)
{
pRecord.CreateInstance(__uuidof(Recordset));
try
{
pRecord->Open(sql.c_str(),
pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
return true;
}
catch (...)
{
return false;
}
}
// 关闭Access数据库的ADO数据集
static bool closeRs(_RecordsetPtr& pRecord)
{
try
{
if (pRecord != NULL)
{
pRecord->Close();
pRecord = NULL;
}
}
catch (...)
{
return false;
}
return true;
}