OLE DB: How woudl you get the number of all rows in database table?

Posted on 2007-10-18
Last Modified: 2012-06-21
Hi experts,

I need to get the number of records in the database table using OLE DB. (The goal is to initialize virtual list view control that is going to display the content of the table.) What are (all) possible ways (via OLE DB)?

Question by:pepr
    LVL 28

    Author Comment

    The related 500 pt question can be found here http:Q_22901047.html
    LVL 142

    Accepted Solution

    you have 2 main options:

    * create a procedure that returns both the count AND the table contents into 2 recordset:

    create procedure get_table_data
    select count(*) from yourtable
    select * from yourtable

    * only issue the SELECT * FROM tablename, and use the RecordCount property:

    LVL 28

    Author Comment

    Well, I should have emphasized that I want to use OLE DB. The RecordCount property is not available in OLE DB. It possibly belong to ADO or some .NET data source...

    Also, I want to access the free DBF table using the VFPOLEDB provider. It should not be that important here; anyway, procedures are not that usual for working with DBF tables (I guess). Moreover, I want to do it as efficiently as possible.

    I have probably found the solution using OLE DB Dataset + session + table objects (the C++ templates), i.e. without using an SQL command. I need more eyes to find the flaws in the following code. I have removed hr testing for brevity. Still, the hr values are visible in debugger...

    #include <iostream>  // cin, cout, cerr
    #include <atldbcli.h>  // C++ OLE DB templates

    int main()

        CDataSource ds;
        CSession session;
        CTable<CDynamicStringAccessor> table;

        // Open the objects.
        CDBPropSet ps_init(DBPROPSET_DBINIT);
        ps_init.AddProperty(DBPROP_INIT_DATASOURCE, "d:/Data");
        HRESULT hr = ds.Open("VFPOLEDB.1", &ps_init);

        hr = session.Open(ds);

        // IRowsetLocate must be supported for getting the count of rows.
        CDBPropSet ps_rowset(DBPROPSET_ROWSET);
        ps_rowset.AddProperty(DBPROP_IRowsetLocate, true);
        hr = table.Open(session, "mytable", &ps_rowset, 1);

        // The count of rows can be obtained this way.
        DBCOUNTITEM count = 0;
        hr = table.GetApproximatePosition(NULL, NULL, &count);
        std::cout << count << std::endl;

        // Close the objects.


        return 0;

    Please, tear it to pieces.  ;)

    LVL 28

    Author Comment

    The above mentioned related question was removed and replaced by another 500 pt question http:Q_22901521.html
    LVL 8

    Assisted Solution

    A more simple way would be

    Instead of passing  the sql statement Pass

    Execscript('use tablename'+chr(13)+chr(10)+'myvar = reccount()'+chr(13)+chr(10)+'select m.myvar from tablename where recno() = 1'+chr(13)+chr(10)+'use')
    LVL 28

    Author Comment

    To suhashegde: Thanks. Well, it may look easier from a programmer point of view. But I need to solve it using OLE DB programming interface. I also mean "simple way" in the technical sense (i.e. as fast and as few operations as possible).

    The solution with table.GetApproximatePosition() above is fine and simple for me. I only need to find whether an apparently simpler solution (using OLE DB) exists or whether there is some clearly visible bug in the solution. I am very new to OLE DB.

    Thanks anyway.
    LVL 28

    Author Comment

    angelIII, suhashegde: I understand both your solutions. While angelIII shows the tradition SQL query approach, suhashegde shows the FoxPro commands for doing the same. I need something else (using OLE DB programming interface). Anyway, I will wait a bit more and then split the points for you with B grade.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Several part series to implement Internet Explorer 11 Enterprise Mode
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now