vc 使用odbc连接mysql数据库分页查询

来源:转载

首先获取记录总数

  1. long nCount = 0;  
  2. try  
  3. {  
  4.   if(m_pDatabase && m_pDatabase->IsOpen())  
  5.   {  
  6.     CString cmdSQL = _T("");  
  7.     cmdSQL.Format( _T("SELECT COUNT(*) FROM file WHERE nID=%ld AND bActive = 1"), nID); //查询条件  
  8.   
  9.     CRecordset rsCount(m_pDatabase);  
  10.     if(rsCount.Open(CRecordset::forwardOnly, cmdSQL, CRecordset::executeDirect))  
  11.     {  
  12.       CString strValue = _T("0");  
  13.       if (!rsCount.IsEOF())  
  14.         rsCount.GetFieldValue((short)0, strValue);  
  15.       nCount = _ttol(strValue);  
  16.       rsCount.Close();  
  17.     }  
  18.   }  
  19. }  
  20. catch(CDBException * e)  
  21. {  
  22.   e->ReportError();  
  23.   e->Delete();  
  24. }  
  然后分页查询
m_nAllPage=(nCount-1)/m_nCountEveryPage+1; //RecordFileSet.Close(); cmdSQL.Format(_T("select id,devid,ftpid,devname,starttime,endtime,recorddir,recordname from tric_video_rdfile order by id ASC limit %d,%d " ),m_nCurPage*m_nCountEveryPage,m_nCountEveryPage); CRecordset rsCount(CDllMainDlg::pDB); try { if(!rsCount.Open(CRecordset::snapshot, cmdSQL, CRecordset::readOnly)) { AfxMessageBox(_T("查询失败")); return; } } catch(CDBException * e) { e->ReportError(); e->Delete(); } CString strValue = _T("0"); while (rsCount.IsEOF()!=TRUE) { rsCount.GetFieldValue((short)0, strValue); recordInfo.Id=_ttoi(strValue); rsCount.GetFieldValue((short)1, strValue); recordInfo.DeviceId=_ttoi(strValue); rsCount.GetFieldValue((short)2, strValue); recordInfo.FtpId=_ttoi(strValue); rsCount.GetFieldValue((short)3,recordInfo.DeviceName); rsCount.GetFieldValue((short)4,strValue); CStringToCtime(recordInfo.StartTime,strValue); rsCount.GetFieldValue((short)5,strValue); CStringToCtime(recordInfo.EndTime,strValue); rsCount.GetFieldValue((short)6,strValue); recordInfo.RecordDir=strValue+_T('/'); rsCount.GetFieldValue((short)7,recordInfo.RecordFileName);
 rsCount.MoveNext();<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>rsCount.Close();
GetFieldValue获取时间

CStringToCtime(CTime & time, CString & str)
{
if(str==_T(""))
{
return;
}
COleDateTime   tm;  
tm.ParseDateTime(str);  
SYSTEMTIME   st;  
tm.GetAsSystemTime(st);  
CTime   ct(st);  
time=ct;
}


分享给朋友:
您可能感兴趣的文章:
随机阅读: