c++ 连接ACCESS数据库所需要的东西

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值