在stdafx.h中已经加入
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
下列代码是放在对话框初始化程序中,但始终返回初始化失败
HRESULT hr; _bstr_t source("Provider={SQL Server};Server=mywork;\
  Uid=sa;Pwd=sa;Database=mydata");
_bstr_t user("sa");
_bstr_t pwd("sa");
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; thanks

解决方案 »

  1.   

    1.你的驱动程序选择的有问题,不能那样使用。可以先找找相关的连接。
    2.hr作为返回值,有时没有办法成功,在MSDN有具体的说明。
    3.需要先看看相关的资料,在www.baidu.com中有内容
      

  2.   

    #import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")void CADOConn::OnInitDBConnect()
    {
    CString strUser;
    CString strServer;
    CString strPwd;
    CString strConnection;
    strUser = theApp.m_UserName;
    strPwd = theApp.m_Password;
    strServer = theApp.m_ServerName; strConnection = "Provider=SQLOLEDB;Server=";
    strConnection+= strServer + ";Database=troublecheck;uid=";
    strConnection+= strUser + ";pwd=";
    strConnection+= strPwd + ";";

    ::CoInitialize(NULL);
    try
    {
    m_pConnection.CreateInstance(__uuidof(Connection));
    _bstr_t strConnect = (_bstr_t)strConnection;
    m_pConnection->Open(strConnect,"","",adModeUnknown);
    //数据库连接成功
    theApp.bConnectDB = TRUE;

    } catch(_com_error e)
    {
    //数据库连接失败
    theApp.bConnectDB = FALSE;
    //AfxMessageBox("failed");
    //AfxMessageBox(e.Description()+"系统退出!");

    }
    }就这样很好用的