none
菜鸟在线等 C++连接数据库出问题 请教高手 RRS feed

  • 问题

  • #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
    
    
     _ConnectionPtr m_pConnection;
     _RecordsetPtr m_pRecordset;
     _CommandPtr m_pCommand;
     HRESULT hr;
     try{
      	CoInitialize(NULL);
      	hr = m_pConnection.CreateInstance("ADODB.Connection");
      	if(SUCCEEDED(hr)) {
       		CString strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Address.mdb";
       		hr = m_pConnection->Open(_bstr_t(strCon.GetBuffer(0)),"","",adModeReadWrite);
    // 		m_pConnection->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Address.mdb";
    // 		hr = m_pConnection->Open("","","",adModeReadWrite);
       		m_pRecordset.CreateInstance("ADODB.Recordset");
       		_variant_t RecordsAffected;
       		m_pRecordset = m_pConnection->Execute("select * from Linkman",&RecordsAffected,adCmdText);
       		m_dataGrid.SetRefDataSource((LPUNKNOWN)m_pRecordset);
       		m_dataGrid.Refresh();
       		_variant_t vIndex = long(0);
       		_variant_t vCount = m_pRecordset->GetCollect(vIndex);
       		m_pRecordset->Close();
       		m_pConnection->Close();
       		CString strMessage;
       		strMessage.Format("共有%d条记录",vCount.iVal);
       		AfxMessageBox(strMessage);
       		MessageBox("连接成功");
       		CoUninitialize(); 
       		m_dataGrid.UpdateData(false);
      	} 
     }catch(_com_error e) {
     	CString erroraessage;
      	erroraessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
      	AfxMessageBox(erroraessage);
     } 
    
    
    
    
    在open方法的时候抛出异常 是什么原因呢 
    
    
    
    
    
    
    
    
    2010年7月15日 1:39

答案

全部回复