今天写了一段代码,好纠结,搞不清楚_RecordsetPtr记录集的指针是怎么移动的
try
{
m_pRecordset=m_pConnection->Execute(_T("select * from RBB2"),NULL,adCmdText);//获取记录集
if(!m_pRecordset->adoEOF)
{
for(int i=0;i<strArray->GetSize();i++)
{
m_pRecordset->MoveFirst();
var1=(float)m_pRecordset->GetCollect((_variant_t)strArray->GetAt(i));//获取字段记录
while(!m_pRecordset->adoEOF)//把指针移动到最后,取最后一条记录赋给var2
{
var2=(float)m_pRecordset->GetCollect((_variant_t)strArray->GetAt(i));
m_pRecordset->MoveNext();
}
res=var2-var1;
result.Format(_T("%.2f"),res);
stringArray->Add(result);
}
}
else
AfxMessageBox(_T("表内数据为空"));
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}我写的这段代码自己看着都恶心,呵呵,没办法,刚起步。想实现的功能就是用每个字段记录集中的最后一个值减第一个值。但是出现的问题是如果数据库某个字段下有空值,程序就会在var2=(float)m_pRecordset->GetCollect((_variant_t)strArray->GetAt(i));这条语句处中断,求解释啊!!或者帮我改写一下也好啊,谢谢大侠们,对你们来说小菜一碟,谢谢了!!
try
{
m_pRecordset=m_pConnection->Execute(_T("select * from RBB2"),NULL,adCmdText);//获取记录集
if(!m_pRecordset->adoEOF)
{
for(int i=0;i<strArray->GetSize();i++)
{
m_pRecordset->MoveFirst();
var1=(float)m_pRecordset->GetCollect((_variant_t)strArray->GetAt(i));//获取字段记录
while(!m_pRecordset->adoEOF)//把指针移动到最后,取最后一条记录赋给var2
{
var2=(float)m_pRecordset->GetCollect((_variant_t)strArray->GetAt(i));
m_pRecordset->MoveNext();
}
res=var2-var1;
result.Format(_T("%.2f"),res);
stringArray->Add(result);
}
}
else
AfxMessageBox(_T("表内数据为空"));
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}我写的这段代码自己看着都恶心,呵呵,没办法,刚起步。想实现的功能就是用每个字段记录集中的最后一个值减第一个值。但是出现的问题是如果数据库某个字段下有空值,程序就会在var2=(float)m_pRecordset->GetCollect((_variant_t)strArray->GetAt(i));这条语句处中断,求解释啊!!或者帮我改写一下也好啊,谢谢大侠们,对你们来说小菜一碟,谢谢了!!
解决方案 »
- HOOKAPI ReadFile函数中如何根据HANDLE hFile获得读取文件的名称
- 【求教】VC++下多媒体效果编写方法
- 如何在EDIT中設置字體的屬性,比如顏色
- VC里怎样在ActiveX里放标准控件?
- 下面的2个函数输出到打印机上为什么不同?
- 调用sendto()发送数据时老是报10013错误
- 如何用vc自动抓取网页,并分析其内容?(像网络蜘蛛那样?)
- 如何在创建文件是时设定它的图标
- 广州招兼职程序员
- 一个初二的中学生学vc++到精通,需要多长时间?
- C2065 m_szProductDesc1未声明的标识符; C3861 UpdateData 找不到标识符
- 这样的Edit谁会,高手帮忙一下
m_pRecordset->MoveNext();
指针移动
varValue=m_pRecordset->GetCollect(_variant_t(_T("gnumber")));
if (varValue.vt!=VT_NULL)
{number=varValue.intVal;}
else{number=0;}
添加判断条件,改成这样试.
m_pRecordset->MoveNext();
移动指针
while(!m_pRecordset->adoEOF)
判断指针是否移动到记录集的末尾,遍历记录集
var2=(float)m_pRecordset->GetCollect((_variant_t)strArray->GetAt(i));
这句前加判断若字段为空,按照你的要求处理。
可以参考置顶帖的例子
try
{
//你的ADO代码
}
catch (_com_error& e)
{
AfxMessageBox(e.Description());
}