Link to home
Start Free TrialLog in
Avatar of pepr
pepr

asked on

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

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)?

Thanks,
   Petr
Avatar of pepr
pepr

ASKER

The related 500 pt question can be found here http:Q_22901047.html
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr

ASKER

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()
{
    ::CoInitialize(NULL);

    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.
    table.Close();
    session.Close();
    ds.Close();

    ::CoUninitialize();

    return 0;
}
==========================================================

Please, tear it to pieces.  ;)

Thanks,
   Petr
Avatar of pepr

ASKER

The above mentioned related question was removed and replaced by another 500 pt question http:Q_22901521.html
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr

ASKER

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.
Avatar of pepr

ASKER

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.