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)已打开,数据库登录没问题,访问权限没问题。大家帮忙看一下呀,谢谢,十分感谢,急呀,急呀,急呀!!
{
// 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)已打开,数据库登录没问题,访问权限没问题。大家帮忙看一下呀,谢谢,十分感谢,急呀,急呀,急呀!!
_variant_t RecordsAffected;
try
{
m_pConn->Execute( sSql.AllocSysString(),&RecordsAffected,
adCmdText|adExecuteNoRecords );
return TRUE ;
}
catch(_com_error& e)
{
DispAdoError(e); return FALSE;
}
-----------
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()是什么
}
连接数据库失败!
错误信息是:Unknown error 0x800A0E81.
能确定是什么问题吗?希望大家能帮忙看一下,不甚感激!!
//连接不成功,改成这样试试
_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好像错了。
1)建立新表单,引入ADODC控件并设置参数,测试连接是否通过,如果测试失败,说明ADODC的参数设置错误,重新修改设置参数(服务器名称,密码,数据库名称等参数)
2)测试通过后,比较ADODC的参数表达语句和你现在写的有否区别,并修改。
但“未指定的错误”依然存在,麻烦朋友们继续帮忙看一下。
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到的错误是:“未指定的错误”!!
麻烦朋友们继续帮忙看一下!!
改成这样试试
CString strSQL = "select * from authors";
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
pRst->Open(varSQL,varSRC,adOpenStatic,adLockOptimistic,adCmdText);
谢谢mrxwh(大徐),谢谢lfchen(一条晚起的虫) 。
{
// 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块