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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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<CDynamicStringAcces sor> table;
// Open the objects.
CDBPropSet ps_init(DBPROPSET_DBINIT);
ps_init.AddProperty(DBPROP _INIT_DATA SOURCE, "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(DBPR OP_IRowset Locate, true);
hr = table.Open(session, "mytable", &ps_rowset, 1);
// The count of rows can be obtained this way.
DBCOUNTITEM count = 0;
hr = table.GetApproximatePositi on(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
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<CDynamicStringAcces
// Open the objects.
CDBPropSet ps_init(DBPROPSET_DBINIT);
ps_init.AddProperty(DBPROP
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(DBPR
hr = table.Open(session, "mytable", &ps_rowset, 1);
// The count of rows can be obtained this way.
DBCOUNTITEM count = 0;
hr = table.GetApproximatePositi
std::cout << count << std::endl;
// Close the objects.
table.Close();
session.Close();
ds.Close();
::CoUninitialize();
return 0;
}
==========================
Please, tear it to pieces. ;)
Thanks,
Petr
ASKER
The above mentioned related question was removed and replaced by another 500 pt question http:Q_22901521.html
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.GetApproximatePositi on() 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.
The solution with table.GetApproximatePositi
Thanks anyway.
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.
ASKER