问题描述
- 关于vc6.0利用ADO动态链接ACCESS不成功的问题
- void CDBDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
//建立一个对话框,选择mdb文件static char BASED_CODE szFilter[] = ""mbd文件 (*.mdb)|*.mdb|所有文件 (*.*)|*.*||"";char Buffer[MAX_PATH];::GetCurrentDirectory(MAX_PATHBuffer); CFileDialog FileDlg( TRUE NULL NULL OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT szFilterNULL );FileDlg.m_ofn.lpstrInitialDir=Buffer;if(IDOK==FileDlg.DoModal()){ lpstrName=FileDlg.GetPathName();}//连接数据库 _bstr_t strcnn(_T(""Provider=Microsoft.JET.OLEDB.4.0;Data source=lpstrName""));try{ m_pConnection.CreateInstance(__uuidof(Connection)); m_pConnection->Open(strcnn_T("""")_T("""")adModeUnknown); m_pRecordset.CreateInstance(__uuidof(Recordset));}catch (_com_error e){ AfxMessageBox(""连接数据库失败""); return;}
//打开数据库
try
{_bstr_t bstrSQL=""SELECT * FROM defects "";//order by RecordNum desc""; m_pRecordset->Open(bstrSQLm_pConnection.GetInterfacePtr()adOpenDynamicadLockOptimisticadCmdText); while(!m_pRecordset->adoEOF) { m_Grid.InsertItem(0"); m_Grid.SetItemText(00(char*)(_bstr_t)m_pRecordset->GetCollect(""valid"")); m_Grid.SetItemText(01(char*)(_bstr_t)m_pRecordset->GetCollect(""maxpost"")); m_Grid.SetItemText(02(char*)(_bstr_t)m_pRecordset->GetCollect(""maxminor"")); m_Grid.SetItemText(03(char*)(_bstr_t)m_pRecordset->GetCollect(""defecttype"")); m_Grid.SetItemText(04(char*)(_bstr_t)m_pRecordset->GetCollect(""maxval1"")); m_Grid.SetItemText(05(char*)(_bstr_t)m_pRecordset->GetCollect(""length"")); m_Grid.SetItemText(06(char*)(_bstr_t)m_pRecordset->GetCollect(""defectclass"")); m_Grid.SetItemText(07(char*)(_bstr_t)m_pRecordset->GetCollect(""tbce"")); m_Grid.SetItemText(08(char*)(_bstr_t)m_pRecordset->GetCollect(""speedatmaxval"")); m_Grid.SetItemText(09(char*)(_bstr_t)m_pRecordset->GetCollect(""postedspd"")); m_pRecordset->MoveNext(); } m_pRecordset->Close();}catch (_com_error e){ AfxMessageBox(""数据库打开失败"");}
}
解决方案
你那样写根本不会将文件名拼接在连接字符串里
CString dd;
dd.Format(""Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s""lpstrName);
hr = m_pConnect->Open((_bstr_t)dd""adModeUnknown);
具体:
http://www.cnblogs.com/cy163/archive/2007/04/05/700596.html
解决方案二:
lpstrName是变量,你不能直接放到strcnn中,需要用Format格式化字符串。
解决方案三:
CodeFrge的代码可能解答你的问题http://www.codeforge.cn/article/211683.还有其他更多文章你可以参考的
时间: 2024-12-05 19:40:05