HRESULT CADODB::CreateAccessDB( LPCTSTR strFileName, LPCTSTR strPwd /*= NULL*/ ) { if (NULL == strPwd) strPwd = _T(""); HRESULT hr; LPCTSTR ACCESSDBCNNTEMPLATE = _T("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=%s;") _T("Mode=Share Deny None;Extended Properties=\"\";Jet OLEDB:System database=\"\";") _T("Jet OLEDB:Registry Path=\"\";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;") _T("Jet OLEDB:Database Password=%s;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;") _T("Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;") _T("Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"); CString strCnnString; strCnnString.Format(ACCESSDBCNNTEMPLATE, strFileName, strPwd); ADOX::_CatalogPtr pCatalog; hr = pCatalog.CreateInstance(__uuidof(ADOX::Catalog)); if (FAILED(hr)) _com_issue_error(hr); pCatalog->Create(_bstr_t(strCnnString)); pCatalog.Release(); return S_OK; }
MDB_ACDB::MDB_ACDB() { string cdrdbpath =CDRUtil::GetSelfDirectory() + "cdr.mdb"; WIN32_FIND_DATA fd; HRESULT hr = S_OK; HANDLE hFind = FindFirstFile(cdrdbpath.c_str(), &fd); m_ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + cdrdbpath; m_pConnection.CreateInstance(__uuidof(Connection)); if(hFind != INVALID_HANDLE_VALUE) { m_pConnection->Open(m_ConnectString.c_str(),"","",adModeUnknown); return; } try { ADOX::_CatalogPtr pCatalog = NULL; variant_t RecordsAffected; hr = pCatalog.CreateInstance(__uuidof (ADOX::Catalog)); if(FAILED(hr)) _com_issue_error(hr); else pCatalog->Create(_bstr_t(m_ConnectString.c_str())); //Create mdb m_pConnection->Open(m_ConnectString.c_str(),"","",adModeUnknown); m_pConnection->Execute(sqlstr, &RecordsAffected, adCmdText); m_pConnection->Close(); } catch(_com_error &e) { //打印日志至文件 sprintf(ErrorMsg,ERROR_MSG_MDB_CONNECT,m_ConnectString.c_str(),e.ErrorMessage(),e.Description()); WriteLog(ErrorMsg,E); return; } }