ActiveX Data Objects (ADO) enable you to access and manipulate data from a variety of different sources. Through ADO, you can connect to various databases such as Access, mySQL, PostgreSQL, MSSQL, Oracle and more. Once connected you can execute SQL against the database, as per normal.
ADO provides a series of objects, methods and properties to make your life as a developer easier. One such object is the RecordSet
, which provides a GetRows
method. GetRows
purpose is to copy a series of records from a populated RecordSet
object and place them into an array. You can specify the number of rows to copy, where to start copying from and what fields in the RecordSet
you’d like copied. The general format is as follows:
myArray = myRecordSet.GetRows(<number of rows>, <starting position>, <fields>)
After consulting the documentation for the method on MSDN, it appears that calling the GetRows
method actually advances the current cursor position in the RecordSet
. As an example, consider a RecordSet
with the cursor position pointing to the first item. After calling GetRows
with a three (3) for the first parameter, the next time you read the RecordSet
the cursor will be pointing at the fourth record.
Unfortunately, I was caught out by this feature. Without consulting any documentation on the method first, I expected that calling GetRows
would copy an arbitrary number of records from any position in the RecordSet
without altering the current cursor position. If it did need to move the cursor to access the rows, I expected that it would return the cursor to the original position.
This is really just a fine implementation issue. I felt that since it was a copy type method, that it wouldn’t have made any modification to the RecordSet
object. If you hadn’t of read the documentation, would you expect that it would advance the cursor or leave it in its original position?