ADO程序运行出现Runtime Error

mftianmiu 2006-05-07 10:42:28
ADO程序运行总是出现这种错误:Runtime Error! This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

代码如下:

void CAdoDlg::OnBtnQuery()
{
// TOD Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
pConn->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=maofeng;Initial Catalog=pubs;Data Source=MAOFENG\\\\MF_SQL";
pConn->Open("","","",adAsyncConnect);

pRst=pConn->Execute("select * from authors",NULL,adCmdText);
while(!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("au_lname"));
pRst->MoveNext();
}

pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
}
编译没问题!!!
经调试,发现错在pRst=pConn->Execute("select * from authors",NULL,adCmdText);这一句。

注:SQL(MAOFENG\MF_SQL)已打开,数据库登录没问题,访问权限没问题。

大家帮忙看一下呀,谢谢,十分感谢,急呀,急呀,急呀!!
...全文
564 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
MajorVon 2006-11-08
  • 打赏
  • 举报
回复
void CAdoDlg::OnBtnQuery()
{
// TOD Add your control notification handler code here
CoInitialize(NULL);
_RecordsetPtr pRst(__uuidof(Recordset));
pRst->Open("select * from authors",
"Provider=SQLOLEDB.1; Persist Security Info = False;
User ID=maofeng;Initial Catalog=pubs;Data Source=MAOFENG\\\\MF_SQL",
adOpenStatic,
adLockOptimistic,
adCmdText);

if(pRst->BOF && pRst->adoEOF)
{
pRst->Close();
}

while(!pRst->rsEOF)
{
if(pRst->GetCollect("au_lname").vt != VT_NULL)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(pRst->GetCollect("au_lname").bstrVal);
}
pRst->MoveNext();
}

pRst->Close();
CoUninitialize();
}
稍作修改,不过还要加try-catch块
MajorVon 2006-11-08
  • 打赏
  • 举报
回复
多看看人家的工程代码,
你这个完全是抄书上的代码.
mftianmiu 2006-11-07
  • 打赏
  • 举报
回复
后来重新安装SQL Server以后搞定了。
谢谢mrxwh(大徐),谢谢lfchen(一条晚起的虫) 。
mftianmiu 2006-05-09
  • 打赏
  • 举报
回复
还是不行呀,
一条晚起的虫 2006-05-09
  • 打赏
  • 举报
回复
pRst=pConn->Execute("select * from authors",NULL,adCmdText);
改成这样试试
CString strSQL = "select * from authors";
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
pRst->Open(varSQL,varSRC,adOpenStatic,adLockOptimistic,adCmdText);
mftianmiu 2006-05-09
  • 打赏
  • 举报
回复
单步跟踪,发现错误出现在这个函数:
inline HRESULT Connection15::Open ( _bstr_t ConnectionString, _bstr_t UserID, _bstr_t Password, long Options ) {
HRESULT _hr = raw_Open(ConnectionString, UserID, Password, Options);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _hr;
}
_hr的值为:-2147467259。
出这个函数以后catch到的错误是:“未指定的错误”!!
麻烦朋友们继续帮忙看一下!!

mftianmiu 2006-05-09
  • 打赏
  • 举报
回复
我从ADODC控件内得到的连接字符串和我先前用的是一模一样的,所以就目前来看connectiongstring是没什么问题的。
但“未指定的错误”依然存在,麻烦朋友们继续帮忙看一下。
mftianmiu 2006-05-09
  • 打赏
  • 举报
回复
多谢大家的帮助,但是还是不行呀,大家能帮忙再看一下吗?
dasiu 2006-05-09
  • 打赏
  • 举报
回复
还有问题的话,使用ADODC控件(加入新控件到表单中)测试一下
1)建立新表单,引入ADODC控件并设置参数,测试连接是否通过,如果测试失败,说明ADODC的参数设置错误,重新修改设置参数(服务器名称,密码,数据库名称等参数)
2)测试通过后,比较ADODC的参数表达语句和你现在写的有否区别,并修改。
dasiu 2006-05-08
  • 打赏
  • 举报
回复
可能是你的第2个参数有问题:
_variant_t RecordsAffected;
try
{
m_pConn->Execute( sSql.AllocSysString(),&RecordsAffected,
adCmdText|adExecuteNoRecords );
return TRUE ;
}
catch(_com_error& e)
{
DispAdoError(e);

return FALSE;
}
一条晚起的虫 2006-05-08
  • 打赏
  • 举报
回复
pConn->Open("","","",adAsyncConnect);
//连接不成功,改成这样试试
_bstr_t ConnectionString="Provider=SQLOLEDB.1;Server=MAOFENG; Persist Security Info=False;Uid=maofeng;psw=;Initial Catalog=pubs;Data Source=MF_SQL";
pConn->Open(ConnectionString, "", "",adUnKnown);
------
你的server和data source好像错了。
mftianmiu 2006-05-08
  • 打赏
  • 举报
回复
错误是:
       连接数据库失败!
       错误信息是:Unknown error 0x800A0E81.
能确定是什么问题吗?希望大家能帮忙看一下,不甚感激!!
一条晚起的虫 2006-05-08
  • 打赏
  • 举报
回复
数据库操作常会抛出异常,请用try{}catch{}捕获。不知道错误原因,就很难处理。
-----------
try
{
pConn->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=maofeng;Initial Catalog=pubs;Data Source=MAOFENG\\\\MF_SQL";
pConn->Open("","","",adAsyncConnect);

pRst=pConn->Execute("select * from authors",NULL,adCmdText);
while(!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("au_lname"));
pRst->MoveNext();
}

pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
}
catch(_com_error e)
{
//看看e.error(),e.ErroeMessage()是什么
}
mftianmiu 2006-05-08
  • 打赏
  • 举报
回复
照上面改了以后还是不行呀,

4,012

社区成员

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

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