首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

用ADO连接sqlsever失败,该如何处理

2012-05-02 
用ADO连接sqlsever失败求高手,看这几句哪里出错了,我的编译环境是vs2008m_pConnection.CreateInstance(AD

用ADO连接sqlsever失败
求高手,看这几句哪里出错了,我的编译环境是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)初始化了

[解决办法]
加异常捕获看是什么错误

C/C++ code
try{//你的ADO代码}catch (_com_error& e){CString strMsg;strMsg.Format(_T("错误描述:%s\n错误消息%s",         (LPCTSTR)e.Description(),        (LPCTSTR)e.ErrorMessage());AfxMessageBox(strMsg);}
[解决办法]
如果是sql server 2000,试试这个(vs2010 unicode)
C/C++ code
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;} 

热点排行