如何在vc6中使用ADO的errors集合捕获错误?

柳长街 2003-09-13 02:53:57
如何在vc6中使用ADO的errors集合捕获错误?

如果本人用以下代码建立oracle连接:执行记录集的AddNew()方法时出错!
CString strConn;
strConn="Provider=MSDAORA.1;Password=sde;User ID=sde;";
strConn+="Data Source=xymap";
m_pConn->Open(_bstr_t(strConn),_bstr_t(L"sde"),
_bstr_t(L"sde"),adModeUnknown);

而用dsn建立连接对象时,执行记录集的AddNew()方法时正常,不知为什么?
m_pConn->Open(_bstr_t(L"DSN=zjr_ado"),_bstr_t(L"sde"),
_bstr_t(L"sde"),adModeUnknown);

我想用_com_error类型捕获错误,可是try一下跳到了catch(...)处。。。愁阿 !
请高手指教?
本人不胜感激!
qq:94643913
msn:chinaflys
mobile phone:13936671884
找张佳仁
...全文
103 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingzai 2003-09-13
  • 打赏
  • 举报
回复
we need use Errors from connection object. Following is the code
example from MSDN..


///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////

void PrintProviderError(_ConnectionPtr pConnection)
{
//Define Other Variables
HRESULT hr = S_OK;
_bstr_t strError;
ErrorPtr pErr = NULL;

try
{
// Enumerate Errors collection and display
// properties of each Error object.
long nCount = pConnection->Errors->Count;

// Collection ranges from 0 to nCount - 1.
for(long i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error #%d\n", pErr->Number);
printf("\t %s\n",(LPCSTR)pErr->Description);
printf("\t(Source: %s)\n",(LPCSTR)pErr->Source);
printf("\t(SQL State: %s)\n",(LPCSTR)pErr->SQLState);
printf("\t(NativeError: %d)\n",(LPCSTR)pErr->NativeError);
if ((LPCSTR)pErr->GetHelpFile() == NULL)
{
printf("\tNo Help file available\n");
}
else
{
printf("\t(HelpFile: %s\n)" ,pErr->HelpFile);
printf("\t(HelpContext: %s\n)" , pErr->HelpContext);
}
}
}
catch(_com_error &e)
{
// Notify the user of errors if any.
PrintComError(e);
}
}

///////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
///////////////////////////////////////////////////////////

void PrintComError(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());

// Print Com errors.

printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
ccwing 2003-09-13
  • 打赏
  • 举报
回复
给的代码不完整。

先确保你的m_pConn有没有调用CreateInstance创建
用下面代码看看

_bstr_t strDsn,str_db_user,str_db_pwd;
_ConnectionPtr pConnection;
...
try{
pRecordset.CreateInstance("ADODB.RecordSet");
pConnection->Open(strDsn,str_db_user,str_db_pwd,adConnectUnspecified);
}
catch(_com_error e){
if(pConnection!=NULL && pConnection->State){
pConnection->Close();
}
pConnection=NULL;
}

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧