怎么样可以得到记录数?(ADO)

qiujoe 2000-08-29 11:14:00
我用pRs->GetRecordSet()或pRs->RecordSet;都不能得到记录数,所得值为-1.
怎么样才能得到,是不是要遍历一次才行?
...全文
465 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
halbert 2000-11-22
  • 打赏
  • 举报
回复
关于vc++ 的ado 编程
atdafx.h
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")

BOOL CGetemailApp::InitInstance()
{
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("ole 出错");
return FALSE;
}
::CoInitialize( NULL );
}
int CGetemailApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
::CoUninitialize();
return CWinApp::ExitInstance();
}

bool getrecordset()
{
char SqlStr[255],account[50];
char Error[500];
char putfilestr[300];
long recordnum;
m_pConnection = NULL;
m_pRecordset = NULL;
_variant_t TheEmail,ThePhone_office,TheAccount,TheUser_id;
char strEmail[50],strPhone_offiece[50],strAccount[50],strUser_id[50];
if(!OpenFileForReadWrite())
return ;
try{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
recordnum=m_pConnection->Open("DSN=stat","test","test",-1);
sprintf(putfilestr,"%20s %30s %40s %30s\n","用户编码","用户帐号","用户Email","用户办公电话");
fwrite(putfilestr,strlen(putfilestr),1,fpout);
while(!feof(fpin))
{
fscanf(fpin,"%s",account);
//sprintf(SqlStr,"select a.user_id,a.account, b.email,b.phone_office from userbasicinfo a,usercontactinfo b where a.account='%s' and b.user_id=a.user_id",account);
strcpy(SqlStr,"select * from region");
recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
//用 recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
// 也出错
recordnum=m_pRecordset->adoEOF;
//执行到此出错,为何。跳到catch(...)
if(!m_pRecordset->adoEOF)
{
TheUser_id=m_pRecordset->GetCollect("user_id");

TheAccount=m_pRecordset->GetCollect("account");
TheEmail=m_pRecordset->GetCollect("email");
ThePhone_office=m_pRecordset->GetCollect("phone_office");
sprintf(putfilestr,"%20s %30s %40s %30s\n",TheUser_id.bstrVal,TheAccount.bstrVal,TheEmail.bstrVal,ThePhone_office.bstrVal);
fwrite(putfilestr,strlen(putfilestr),1,fpout);
}//end if
m_pRecordset->Close();
}//end while
}//end try
catch(_com_error *e)
{

//CString Error = e->ErrorMessage();
//char Error[500];
strcpy(Error , (char*)e->Description());
AfxMessageBox(e->ErrorMessage());
}
catch(_com_error e)
{

//CString Error = e->ErrorMessage();
strcpy(Error , (char*)e.Description());
//AfxMessageBox(e->ErrorMessage());
AfxMessageBox(Error);
}
catch(...)
{
AfxMessageBox("ado 出错");
}

m_pRecordset->Close();;
m_pConnection->Close();
m_pConnection->Release();
CloseAllFile();
}
// 执行到recordnum=m_pRecordset->adoEOF; 出错 为何
// 请那位大侠指点

qiujoe 2000-09-01
  • 打赏
  • 举报
回复
谢谢!
lynxliu 2000-08-29
  • 打赏
  • 举报
回复
不用像他们说的那么复杂,你所使用的ado可以在recordset里使用recordCount获得记录数,但是要求所使用的不可以是向前游标和动态游标,这两种游标会返回-1。open方法的第3个参数是游标类型,设置为adOpenStatic和adOpenKeyset(或者写1和2)就可以了。也可以先设置游标类型,如rs->cursortype=1
对于任何支持ole db的数据库都可以。
--茶-- 2000-08-29
  • 打赏
  • 举报
回复
打开的记录集时,CursorType使用adOpenKeyset或adOpenStatic,即可通过RecordCount返回记录总数,如果事业adOpenDynamic得到的记录总数为-1;正确的打开记录集的方法如下:
_RecordsetPtr pRsSelf;
pRsSelf.CreateInstance(__uuidof(Recordset));
//pRsSelf->CursorLocation=adUseServer;
pRsSelf->Open("select * from [selfmade] order by 图号",pCon.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);

_RecordsetPtr pRsProd;
pRsProd.CreateInstance(__uuidof(Recordset));
pRsProd->Open("select * from [product] order by 编号",pCon.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);

_RecordsetPtr pRsStd;
pRsStd.CreateInstance(__uuidof(Recordset));
pRsStd->Open("select * from [standard]",pCon.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
......
我使用的是SQL Server7.0做数据库
DragonCheng 2000-08-29
  • 打赏
  • 举报
回复
如果需要得到记录数,你需要对整个记录集滚动一次才能得到。
如果你觉得该种方法耗时,可以先用( select count(*) from 表名)语句得到记录数,然后在操作
qiujoe 2000-08-29
  • 打赏
  • 举报
回复
我用得是sql server7.0是不是不支持这个属性?

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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