求高手,看这几句哪里出错了,我的编译环境是vs2008
m_pConnection.CreateInstance("ADODB.Connection");// 创建Connection对象

_bstr_t strConnect = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=UserMan;Data Source=localhost;";m_pConnection->Open(strConnect,"","",adModeUnknown);一直连接失败,调试跟踪到这一步,就出错了。
我在头文件里导入了#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
而且开始时::CoInitialize(NULL)初始化了

解决方案 »

  1.   

    加异常捕获看是什么错误
    try
    {
    //你的ADO代码
    }
    catch (_com_error& e)
    {
    CString strMsg;
    strMsg.Format(_T("错误描述:%s\n错误消息%s", 
    (LPCTSTR)e.Description(),
    (LPCTSTR)e.ErrorMessage());
    AfxMessageBox(strMsg);
    }
      

  2.   

    我加了异常捕获,弹出的错误是未定义错误!vs2008用mfc能连接sqlsever么?
      

  3.   

    如果是sql server 2000,试试这个(vs2010 unicode)bool Ado::open(CString serv,CString db,CString user,CString pass)
    {
    try{
    HRESULT hr = con.CreateInstance(_uuidof(Connection));
    if(FAILED(hr))
    throw false;
    _bstr_t bs = L"Provider=SQLOLEDB;data source="+ serv +L";Database="+ db +
    L";uid="+ user +L";pwd="+pass+L";";
    hr = con->Open(bs,L"",L"",0);
    if(FAILED(hr))
    throw 1;
    return true;
    }catch(bool){
    AfxMessageBox(L"con.CreateInstance");
    }catch(_com_error &e){
    AfxMessageBox(e.Description());
    }catch(int){
    AfxMessageBox(L"con->Open");
    }
    return false;
    }
      

  4.   

    不是sql server 2000,我是用的 sql sever 2005 express。好吧,我先试试这段代码再说。
      

  5.   

    找到问题的原因了,那个数据源改一下没有弄对,应该是Data Source=localhost\\SQLEXPRESS;,不过还是两位的帮助!