备忘:ado数据连接

ado是建立在oledb上的。

三种ado数据连接:

   CoInitialize(NULL);

  _ConnectionPtr pConn(__uuidof(Connection));
  _RecordsetPtr pRst(__uuidof(Recordset));
  _CommandPtr pCmd(__uuidof(Command));

pConn->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=pubs.mdb;Persist Security Info=False"; 
pConn->Open("","","",adConnectUnspecified);

1:pRst=pConn->Execute("select * from authors",NULL,adCmdText);

2:pRst->Open("select * from authors",_variant_t((IDispatch*)pConn);

3:pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
  pCmd->CommandText="select * from CParameter";
  pRst=pCmd->Execute(NULL,NULL,adCmdText);

功能:

 while(!pRst->rsEOF)
  {
   ((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
    (_bstr_t)pRst->GetCollect("Address"));
   pRst->MoveNext();
  }

末了:

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

 

异常捕获:

catch(_com_error &e)
 {
  CString err;
  err.Format("%s",(char *)(e.Description()));
  AfxMessageBox(err);
 }
 catch (CMemoryException* e)
 {
  TCHAR   szCause[255];//用char和tchar都可以
  CString strFormatted;
  e->GetErrorMessage(szCause, 255);
  
  strFormatted = _T("The data file could not be opened because of this error: ");
  strFormatted += szCause;
  
  AfxMessageBox(strFormatted);

   }

 

 

 

你可能感兴趣的:(连接)