怎样得到某一数据库表中某一字段的最大值?

yifengchanghong 2004-10-14 10:34:54
VC++操作数据库得到某一数据库表中某一字段的最大值:
_ConnectionPtr pConnection;
_RecordsetPtr pRecordset;
_CommandPtr pCommand;

CString strSQL=_T("SELECT max(Section) FROM table");

_variant_t fieldCount;
VariantInit (&fieldCount);

pCommand->CommandType =adCmdText;
pCommand ->CommandText =(_bstr_t)strSQL;
pRecordset= pCommand->Execute (&fieldCount,NULL,adCmdUnknown);

怎样才能把该最大值得出来?往下该如何做?

...全文
181 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huxzjqhh 2004-10-14
  • 打赏
  • 举报
回复
_variant_t TheValue;
TheValue=pRecordset->GetCollect("Section");
快乐鹦鹉 2004-10-14
  • 打赏
  • 举报
回复
获取第一个字段的值啊。GetFieldValue
huxzjqhh 2004-10-14
  • 打赏
  • 举报
回复
_variant_t TheValue;
TheValue=pRecordset->GetCollect("");
内存泄漏 2004-10-14
  • 打赏
  • 举报
回复
不要用pCommand,用pRecordset就可以了:
CString t = "SELECT max(Section) as maxnum FROM table";
pRecordset->Open(_variant_t(t),_variant_t((IDispatch*)pConnection,true),adOpenDynamic,adLockOptimistic,adCmdUnknown);

CString s1 = _com_util::ConvertBSTRToString((_bstr_t)m_pRs->GetCollect("maxnum"));
AfxMessageBox(s1);

s1就是查询出来的最大值。。。。。
yifengchanghong 2004-10-14
  • 打赏
  • 举报
回复
happyparrot(快乐鹦鹉) :
语句该如何写,能否写出来。

huxzjqhh(黑石):
为什么我使用你的语句,程序出现异常。
不知你测试过没有?

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧