读书人

定时器 OnTimer 中使用ADO 查询数据库

发布时间: 2012-01-12 22:11:58 作者: rapoo

定时器 OnTimer 中使用ADO 查询数据库视图结果,并返回结果集到程序中报错
在MFC中定时器 OnTimer 中使用ADO 查询数据库视图结果,并返回结果集到程序中报错
用errormessage.Format(e.Description()) 显示错误:row handle referred to a deleted row marked for deletion.

代码如下:
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
CString cs_sql,cs_subject,cs_body;


cs_sql.Format(_T("select convert(char(19),testtime,120) testtime,jobno,deviceid ,station,description,inputqty,outputqty,yield*100 yield,yieldredline*100 yieldredline,sendto from MSGJobStationAlarm(nolock)"));
//AfxMessageBox(cs_sql);
m_pRecordset->Open(_bstr_t(cs_sql.AllocSysString()),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);

while(!(m_pRecordset->adoEOF))
{
vTesttime = m_pRecordset->GetCollect("testtime");
。。。
m_pRecordset->MoveNext();

}

}

catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(e.Description());
AfxMessageBox(errormessage);///显示错误信息
}

}

如果我的sql 使用的是 select count(*) from ..... 能得到正确的结果,这是为什么啊? 望高手解惑,万分感谢

[解决办法]
你多长时间执行一次?
[解决办法]
在定时器中是没有问题的,做过测试,死循环查询数据库都没事,就是CPU占用高一些。
打开记录集的CursorTypeEnum的adOpenStatic换成adOpenDynamic。

读书人网 >VC/MFC

热点推荐