CDataSave::ConStr()是我拼凑的数据库连接字符串,当这个字符串正确的时候程序一切正常.但是当这个字符串错误的时候,就会弹出错误:
Runtime Error!
Program: E:\wor...
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
代码中捕捉不到这个错误,无解中,特来请教.
代码如下:
_ConnectionPtr pMyConnect = NULL;
try
{
HRESULT hr1 = pMyConnect.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr1))//创建成功
{
HRESULT hr2=pMyConnect->Open(CDataSave::ConStr(),"","",adModeUnknown);///连接数据库
if(!SUCCEEDED(hr2)) return false;//连接失败
}
else
return false;
}
catch (_com_error e) //捕捉异常
{
CString strComError; 
strComError.Format("Error: .lx\nError Message: %s\nSource: %s\nDescription: %s", 
e.Error(),e.ErrorMessage(),(LPCSTR) e.Source(),(LPCSTR) e.Description());
MessageBox(NULL,strComError,"Database Error",MB_ICONEXCLAMATION); 
return false; 
}
return true;

解决方案 »

  1.   

    我的catch 错了吗?请楼上的给点详细的解释 麻烦了
      

  2.   

    用catch (...)也捕捉不到,貌似好像是对的
    举例来说
    CDataSave::ConStr()若是正确的值比如"driver={SQL Server};Server=192.168.1.40;DATABASE=ManageAdmin;UID=sa;PWD="
    时,不会出现任何错误;
    但是这个值若是错误的,比如:"driver={SQL Server};Server=;DATABASE=;UID=;PWD="
    就会出来
    Runtime Error! 
    Program: E:\wor... 
    This application has requested the Runtime to terminate it in an unusual way. 
    Please contact the application's support team for more information. 
    错误.迷茫~!!!
      

  3.   

    你写的这个catch是捕获不到runtime错误的.. 你单步调试一下,看看是哪句出错??
    也可以试试用
    __try{
    ...
    }
    __except(...){
    ...
    }
    看能否捕获..
      

  4.   

    我解决了
    改用_RecordsetPtr不用_ConnectionPtr 
    然后连接
    try..catch
    就好了开心