第一次操作数据库,求指点,谢谢void CRecordDialog::OnRecordView() 
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
_CommandPtr pCmd(__uuidof(Command)); pConn->ConnectionString="Provider=Microsoft.Jet.OLEDB4.0;Data Source=INFO.MDB";
pConn->Open("","","",adConnectUnspecified); pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
pCmd->CommandText="select * from Contact";
pRst=pCmd->Execute(NULL,NULL,adCmdText);
while(!pRst->adoEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("Name"));
pRst->MoveNext();
}

pRst->Close();
pConn->Close();
pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize(); 
}

解决方案 »

  1.   

    pConn->Open("","","",adConnectUnspecified);这个可以这样?
      

  2.   

    尝试下面的代码_ConnectionPtr m_ptrConnection;_RecordsetPtr m_ptrRecordset;_CommandPtr m_ptrCommand;m_ptrConnection.CreateInstance(__uuidof(Connection));m_pRecordset.CreateInstance(__uuidof(Recordset));m_pCommand.CreateInstance(__uuidof(Command));         TCHAR currentDir[1024]={0};         GetCurrentDirectory(1024,currentDir);  //获取当前工作目录         CString connectStr;         connectStr.Format(L"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=%s\\db2.mdb",currentDir);//在当前工作目录下,我的数据库名为db2.mdb         m_ptrConnection->ConnectionString=(_bstr_t)connectStr;         if(m_ptrConnection->Open(_bstr_t(connectStr),"","",adConnectUnspecified) != S_OK)      //成功连接数据库         {              return false;         }
    m_ptrRecordset-> CursorLocation = adUseClient; 
    HRESULT hr = m_ptrRecordset->Open(_bstr_t(L"select* from Main"), _variant_t((IDispatch*)m_ptrConnection, TRUE),adOpenStatic,adLockBatchOptimistic,adCmdUnknown);//以上可以直接复制,以下的就记不太清了,恐怕你要查下MSDN了
    m_ptrRecordset->AddNew();
    …………//这里代码根据根据字符名,设置数据库中新增的数据中各字段的值
    m_ptrRecordset->Update(updatebeatch);//参数应该写对了,这个参数是指批量写入数据库,这句非常重要,如果没有,程序是不会将更新写入数据库的
    然后就是release()和CoUninitialize()了因为不在自己电脑上,MSDN和以前的项目都不在身边,也只能记起这些了,你试一下,如果不行,再问吧
        
      

  3.   


    _ConnectionPtr   m_ptrConnection; _RecordsetPtr   m_ptrRecordset; _CommandPtr   m_ptrCommand; m_ptrConnection.CreateInstance(__uuidof(Connection)); m_pRecordset.CreateInstance(__uuidof(Recordset)); m_pCommand.CreateInstance(__uuidof(Command));                   TCHAR   currentDir[1024]={0};                   GetCurrentDirectory(1024,currentDir);     //获取当前工作目录                   CString   connectStr;                   connectStr.Format(L "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=%s\\db2.mdb ",currentDir);//在当前工作目录下,我的数据库名为db2.mdb                   m_ptrConnection-> ConnectionString=(_bstr_t)connectStr;                   if(m_ptrConnection-> Open(_bstr_t(connectStr), " ", " ",adConnectUnspecified)   !=   S_OK)             //成功连接数据库                   {                             return   false;                   } 
    m_ptrRecordset->   CursorLocation   =   adUseClient;   
    HRESULT   hr   =   m_ptrRecordset-> Open(_bstr_t(L "select*   from   Main "),   _variant_t((IDispatch*)m_ptrConnection,   TRUE),adOpenStatic,adLockBatchOptimistic,adCmdUnknown); //以上可以直接复制,以下的就记不太清了,恐怕你要查下MSDN了 
    m_ptrRecordset-> AddNew(); 
    …………//这里代码根据根据字符名,设置数据库中新增的数据中各字段的值 
    m_ptrRecordset-> Update(updatebeatch);//参数应该写对了,这个参数是指批量写入数据库,这句
    //非常重要,如果没有,程序是不会将更新写入数据库的 
    //然后就是release()和CoUninitialize()了 
      

  4.   

    加上 try catch 看看try
    {
    _ConnectionPtr pConn(__uuidof(Connection));
    _RecordsetPtr pRst(__uuidof(Recordset));
    _CommandPtr pCmd(__uuidof(Command));pConn->ConnectionString="Provider=Microsoft.Jet.OLEDB4.0;Data Source=INFO.MDB";
    pConn->Open("","","",adConnectUnspecified);pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
    pCmd->CommandText="select * from Contact";
    pRst=pCmd->Execute(NULL,NULL,adCmdText);
    while(!pRst->adoEOF)
    {
    ((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
    (_bstr_t)pRst->GetCollect("Name"));
    pRst->MoveNext();
    }
    }
    catch (_com_error &e)
    {
    AfxMessageBox(e.ErrorMessage());
    AfxMessageBox((LPCTSTR)e.Description());
    }
      

  5.   

    感谢热心网友,呵呵
    最后把
    pConn->ConnectionString="Provider=Microsoft.Jet.OLEDB4.0;Data Source=INFO.MDB";
    pConn->Open("","","",adConnectUnspecified);
    这两句替换为
    try                 
    {
    pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=INFO.mdb","","",adModeUnknown);
     
    }
    catch(_com_error e)
    {
    AfxMessageBox("数据库连接失败!");
    }
    就可以了