首页 新闻 会员 周边 捐助

MFC+Access数据库问题

0
悬赏园豆:30 [待解决问题]

BOOL CLog::CreateDataBase(CTime *tmCurrent)
{
if(!tmCurrent) return FALSE;

CloseDataBase();
char DataBaseName[MAX_PATH]; //CEP1: DataBaseName[100]
char PathBaseName[MAX_PATH]; //CEP1: PathBaseName[100]

//CReportDlg *pDlg = (CReportDlg *)AfxGetMainWnd();

sprintf(PathBaseName, "%s\\Log", dirWork);
::CreateDirectory(PathBaseName, NULL);
sprintf(DataBaseName, "%s\\Log\\Log%04d%02d%02d.mdb", dirWork, tmCurrent->GetYear(), tmCurrent->GetMonth(), tmCurrent->GetDay());
CString strConnect;
strConnect.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"), DataBaseName);
COleVariant Connect(strConnect);

try
{
ADOX::_CatalogPtr pCatalog = NULL;
pCatalog.CreateInstance(_T("ADOX.Catalog"));
pCatalog->Create((LPCTSTR)strConnect);

//这里为什么会抛出异常,建立连接不了数据库??

pConnection.CreateInstance(_T("ADODB.Connection"));
pConnection->PutCommandTimeout(30);
pConnection->PutConnectionTimeout(30);
pConnection->put_CursorLocation(ADODB::adUseClient);
pConnection->Open(_bstr_t(strConnect),_bstr_t(),_bstr_t(),ADODB::adConnectUnspecified);
pCatalog->PutActiveConnection(_variant_t((IDispatch *)pConnection));

ADOX::_TablePtr pTableRadar = NULL;
pTableRadar.CreateInstance(_T("ADOX.Table"));
pTableRadar->ParentCatalog = pCatalog;
pTableRadar->Name = "雷达日志";
ADOX::ColumnsPtr pColsRadar = pTableRadar->Columns;
//pColsRadar->Append(_T("原文件名时间"), ADOX::adDate, 0);
pColsRadar->Append(_T("原文件名"), ADOX::adWChar, 0);
pColsRadar->Append(_T("压缩后文件名"), ADOX::adWChar, 128);
//pColsRadar->Append(_T("压缩后时间"), ADOX::adDate, 0);
pColsRadar->Append(_T("文件发送时间"), ADOX::adDate, 0);
pColsRadar->Append(_T("上传是否成功"), ADOX::adWChar, 0);

//pColsRadar->GetItem(_T("原文件名时间"))->Properties->GetItem(_T("Jet OLEDB:Compressed UniCode Strings"))->Value = true;
pColsRadar->GetItem(_T("原文件名"))->Properties->GetItem(_T("Jet OLEDB:Compressed UniCode Strings"))->Value = true;
pColsRadar->GetItem(_T("压缩后文件名"))->Properties->GetItem(_T("Jet OLEDB:Compressed UniCode Strings"))->Value = true;
//pColsRadar->GetItem(_T("压缩后时间"))->Properties->GetItem(_T("Jet OLEDB:Compressed UniCode Strings"))->Value = true;
pColsRadar->GetItem(_T("文件发送时间"))->Properties->GetItem(_T("Jet OLEDB:Compressed UniCode Strings"))->Value = true;
pColsRadar->GetItem(_T("上传是否成功"))->Properties->GetItem(_T("Jet OLEDB:Compressed UniCode Strings"))->Value = true;

pCatalog->Tables->Append(_variant_t ((IDispatch*)pTableRadar));

ADOX::_TablePtr pTableNote = NULL;
pTableNote.CreateInstance(_T("ADOX.Table"));
pTableNote->ParentCatalog = pCatalog;
pTableNote->Name = "场站日记";
ADOX::ColumnsPtr pColsNote = pTableNote->Columns;
pColsNote->Append(_T("内容"), ADOX::adWChar, 255);
pColsNote->GetItem(_T("内容"))->Properties->GetItem(_T("Jet OLEDB:Compressed UniCode Strings"))->Value = true;
pCatalog->Tables->Append(_variant_t ((IDispatch*)pTableNote));

pCatalog->Tables->Refresh();
tmOpened = *tmCurrent;
bOpened = TRUE;
}
catch(_com_error &e)
{
return FALSE;
}
catch(...)
{
return FALSE;
}

return TRUE;
}
//CreateDataBase()结束
//--------------------------------------------------------------------------------------------------

追梦人06的主页 追梦人06 | 初学一级 | 园豆:172
提问于:2017-02-21 16:01
< >
分享
所有回答(1)
0

CreateDirectory 直接生成的路径,你的mdb文件access本身可以打开吗,如果生成一个空文件是无法打开的

2012 | 园豆:21645 (高人七级) | 2017-02-21 18:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册