CoInitialize(NULL);
   try
   {
      _ConnectionPtr pConn("ADODB.Connection");
      _RecordsetPtr  pRst("ADODB.Recordset");      pConn->Open("Provider=sqloledb;Data Source=Server1;"
         "Initial Catalog=test1;User Id=sa;Password=sa;",
         "", "", adConnectUnspecified);
// Note 1.
      pRst->Open(
         "userinfo",
         _variant_t((IDispatch *) pConn, true),
         adOpenStatic,
         adLockReadOnly,
         adCmdTable);
      pRst->MoveLast();
// Note 2.
      printf("Last name is '%s %s'\n",
            (char*) ((_bstr_t) pRst->GetFields()->GetItem("username")->GetValue()),
            (char*) ((_bstr_t) pRst->Fields->Item["otherinfo"]->Value));      pRst->Close();
      pConn->Close();
   }
   catch (_com_error &e)
   {
      printf("Description = '%s'\n", (char*) e.Description());
   }
::CoUninitialize();

解决方案 »

  1.   

    在头文件中加入:
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace  rename("EOF","adoEOF")
    在文件中void CADO100View::OnInitialUpdate() 
    {
    CView::OnInitialUpdate();

    // TODO: Add your specialized code here and/or call the base class
    HRESULT hr;
    _bstr_t source("Driver={SQL Server};Server=REDFOX;\
    Uid=sa;Pwd=57190;Database=公司");
    _bstr_t user("dbo");
    _bstr_t pwd("");
    try{
    hr=m_connection.CreateInstance(_uuidof(Connection));
    if(SUCCEEDED(hr))
    hr=m_connection->Open(source,user,pwd,16);
    if(SUCCEEDED(hr))
    hr=m_recordset.CreateInstance(_uuidof(Recordset));
    if(SUCCEEDED(hr))
    m_fConnected=TRUE;
    else
    m_fConnected=FALSE;
    }
    catch(_com_error &e){
    MessageBox(e.ErrorMessage());
    m_fConnected=FALSE;
    }
    if(!m_fConnected) MessageBox("ADO 数据源初始化失败!");
    else m_strSource=(const char *) source;
    }
    为什么总是连不上?