bool get_resultsettmp(_RecordsetPtr &ors,const char *SqlStatement)
{
if(!ors)
return false;
try
{
if(ors->State == adStateOpen)
{
ors->Close();
}
//ors->CursorLocation = adUseClient; ors->Open(SqlStatement,
m_Connection.GetInterfacePtr(),
adOpenForwardOnly,
adLockOptimistic,
adCmdText);
return true;
}
catch(_com_error &e)
{
ErrorReport("异常语句:%s", SqlStatement);
dump_com_error(e,"get_resultsettmp");
}
catch(...)
{ }
return false;
}
CString sql;
//char sql[1024];
_RecordsetPtr rs;
int MaxID=0;
sql.Format("select max(PlayListID) as ID FROM PlayList where DevCode='%s'", (LPCTSTR)DevCode);
rs.CreateInstance(__uuidof(Recordset));
if (get_resultsettmp(rs,(LPCTSTR)sql)==false)
{
freerecordsetptr(rs);
return 0;
}
else
{
ErrorReport("RecordCount:%d",(int)rs->GetRecordCount());
if ((int)rs->GetRecordCount()==0) //此处返回值一直是-1
{
ErrorReport("RecordCount:%d",rs->GetRecordCount());
return 0;
}
else
{
CString strFieldName="";
strFieldName="ID";
FieldsPtr fp=rs->Fields;
if (fp->Item[_variant_t(strFieldName)]->Value.vt!=VT_NULL)
{
return fp->Item[_variant_t(strFieldName)]->Value;
}
}
}
用rs->eof||rs->bof怎么也提示错误啊
在
rs.CreateInstance(__uuidof(Recordset));
前增加一句试试呢?
CoInitialize(NULL);
while操作记录集可以这样
while(!ors->adoEOF) //不为空则提取数据
if单条语句的操作可以
if(ors->RecordCount > 0) //不为空
try
{
//你的ADO代码
}
catch (_com_error& e)
{
CString strMsg;
strMsg.Format(_T("错误描述:%s\n错误消息%s"),
(LPCTSTR)e.Description(),
(LPCTSTR)e.ErrorMessage());
AfxMessageBox(strMsg);
}
捕获异常为NULL,RecordCount为-1,adoEOF和adoBOF都为0
if(!m_pLastTime->adoEOF)
{
MessageBox((const char*)(_bstr_t)m_pLastTime->RecordCount);
MessageBox((const char*)(_bstr_t)m_pLastTime->adoEOF);
MessageBox((const char*)(_bstr_t)m_pLastTime->adoBOF);
try{
bLst.Format(_T((const char*)(_bstr_t)m_pLastTime->GetCollect("maxt")));}
catch (_com_error& e) { CString strMsg; strMsg.Format(_T("错误描述:%s\n错误消息%s"), (LPCTSTR)e.Description(), (LPCTSTR)e.ErrorMessage()); AfxMessageBox(strMsg); }目测跟查询SQL有关,我的是这样的,和楼上的同一个问题,SQL里都是取max()
select max(v_time) as maxt from v_record where v_cust_num='123456'
怎么解决呢?