[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2007-10-18
Medium Priority
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
  • 5
LVL 29

Author Comment

ID: 20099200
The related 500 pt question can be found here http:Q_22901047.html
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 750 total points
ID: 20099615
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 29

Author Comment

ID: 20099936
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.  ;)

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 29

Author Comment

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

Assisted Solution

suhashegde earned 750 total points
ID: 20122445
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 29

Author Comment

ID: 20122629
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 29

Author Comment

ID: 20122653
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

872 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