Problems with CTable<>::MoveToBookmark() -- sample not working
- From: Petr Prikryl <Petr Prikryl@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 23 Oct 2007 07:05:02 -0700
Hi experts,
I am learning OLE DB, using the VFPOLEDB provider. I have problems with
jumping to absolute position in a rowset. From what I have learned so far, I
should be able to set a bookmark and then move relatively to the bookmarked
row. This also means that when bookmarking the very first row, I should be
able to jump to the n-th row (i.e. to an absolute position).
I am using CDataSource, CSession, CTable<CDynamicStringAccessor, CRowset>,
and CBookmark<> objects. What does not work for me is the sequence:
CBookmark<> bookmark;
hr = table.MoveFirst();
hr = table.GetBookmark(&bookmark); // here S_OK
hr = table.MoveToBookmark(bookmark, 0); // here DB_E_BADBOOKMARK
hr = table.MoveToBookmark(bookmark, 100); // also DB_E_BADBOOKMARK
I am obviously doing something wrong. Just to know what ;) Any idea?
Thanks,
Petr
P.S. The full spike-example follows. The hr values not tested here for
brevity. Look at them in the debugger.
===================================================
#include <iostream> // cin, cout, cerr
#include <atldbcli.h> // C++ OLE DB templates
int main()
{
::CoInitialize(NULL);
// Open the data source, use the VFPOLEDB provider.
CDataSource ds;
CDBPropSet ps_init(DBPROPSET_DBINIT);
ps_init.AddProperty(DBPROP_INIT_DATASOURCE, "d:/Data");
HRESULT hr = ds.Open("VFPOLEDB.1", &ps_init);
// Open the session with the above data source
CSession session;
hr = session.Open(ds);
// Open the table via session object (i.e. no SQL command) thus
// obtaining the open rowset inside. IRowsetLocate must be supported
// for getting the count of rows and moving to absolute positions.
CTable<CDynamicStringAccessor, CRowset> table;
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;
// Moving to the first position and then to n-1
// position gets you to the n-th row in the rowset.
// (The n is zero based.) However, it is uggly.
// Moreover, moving to the zeroth record looks
// strange (i.e. MoveFirst(), MoveNext(-1)).
DBCOUNTITEM n = 0;
hr = table.MoveFirst();
hr = table.MoveNext(n - 1);
// Another jump to position (101st row).
n = 100;
hr = table.MoveFirst();
hr = table.MoveNext(n - 1);
// I was hoping that something like this works...
CBookmark<> bookmark;
hr = table.MoveFirst();
hr = table.GetBookmark(&bookmark); // here S_OK
// And then I could do the following.
hr = table.MoveToBookmark(bookmark, 0); // here DB_E_BADBOOKMARK
hr = table.MoveToBookmark(bookmark, 100); // also DB_E_BADBOOKMARK
// Close the objects.
table.Close();
session.Close();
ds.Close();
::CoUninitialize();
return 0;
}
===================================================
.
- Prev by Date: Re: ldb file remains after closing connection
- Next by Date: Re: OleDB query to Excel returning Null values for numeric data
- Previous by thread: Data link properties dialog and MS Excel 2003
- Next by thread: Trouble connecting to Access DB with ASP on Vista 32bit
- Index(es):