获取 Recordset 对象中的记录数 C++

本文转自:http://dev.firnow.com/course/3_program/c++/cppsl/2008419/110683.html

在 ADO 中,用 Recordset  对象的 Open 方法打开记录集后,就可以用 GetRecordCount 方法获取记录数。

代码如下:

    CString strSQL = "SELECT * FROM authors"; 

    _RecordsetPtr pRs = NULL; 
    pRs.CreateInstance("ADODB.Recordset");

long nRecordCount = 0;

try

        pRs->Open( _variant_t(strSQL),
            m_pConnection.GetInterfacePtr(),
            adOpenStatic,
            adLockOptimistic,
            adCmdText);


        nRecordCount = pRs->GetRecordCount();

        pRs->Close();
    }
catch ( _com_error &e )

        MessageBox( e.Description() ); 
    }

    CString strRecordCount;

    strRecordCount.Format( "RecordCount: %d" , nRecordCount );

    MessageBox( strRecordCount );

注意:

在用 Open 方法打开记录集时使用了 adOpenStatic 游标;如果使用 adOpenDynamic 游标,GetRecordCount 方法将返回 -1 。

我的看法:

使用静态游标时,数据已经被取到了本地内存中,所以可以知道记录数;

而使用动态游标时,只有移动到记录集结尾时才能统计出记录数,所以刚打开记录集时返回 -1 。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值