The function CeSeekDatabase can be used to:
Once the desired record has been located, CeReadRecordPropsEx can be used to read properties from the record.
The key to using this function is the constant value specified in dwSeekType. The following constants can be used to seek to absolute or relative record positions (Table 4.14).
Listing 4.10 shows a call to CeReadRecordPropsEx to locate the last record in the database. Listing 4.10 Locates last record in databasevoid Listing4_10() { HANDLE hDB = Listing4_4(); // open database DWORD dwIndex; if(hDB != INVALID_HANDLE_VALUE) { if(CeSeekDatabase(hDB, CEDB_SEEK_END, 0, &dwIndex)) { cout _T("Record index: ") dwIndex endl; ReadOneProp(hDB); } Listing4_5(hDB); // close database } } CeSeekDatabase returns the record number of the located record, and Listing 4.10 displays this value. The following code fragment locates the last but one record in the database the code seeks 21 records from the end of the database. CeSeekDatabase(hDB, CEDB_SEEK_END, -1, &dwIndex); Seeking by the record's object identifier (CEDB_SEEK_CEOID) is very efficient, so it is a good idea to store the object identifiers of records you need to revisit, and then use CeSeekDatabase to seek using these object identifiers. The remaining constants for dwSeekType use the sort order to locate records. When using these constants, dwValue is a pointer to a CEPROPVAL structure that contains the value to search for. The current record pointer is left at the end of the database if the search fails to locate a record and the function returns 0. The constant values are shown in Table 4. 15.
Of these, CEDB_SEEK_VALUEFIRSTEQUAL is used most frequently to locate records with a specific value, and CEDB_SEEK_VALUEFIRSTEQUAL with CEDB_SEEK_VALUENEXTEQUAL to locate all records with a specific value. Listing 4.11 shows how to list all records with a particular value ("Company 2"). You can run Listing 4.7 a number of times to add extra "Company 2" records to the database. Listing 4.11 Lists all records with the given valuevoid Listing4_11() { HANDLE hDB; CEGUID ceObjStore; DWORD dwIndex; CEOID ceOidDB = 0; CREATE_SYSTEMGUID(&ceObjStore); hDB = CeOpenDatabaseEx(&ceObjStore, &ceOidDB, _T("Company"), propCompany, // prop.id. of sort order 0, // no auto-increment NULL); // no notifications if(hDB != INVALID_HANDLE_VALUE) { CEPROPVAL propSeek; propSeek.propid = propCompany; propSeek.val.lpwstr = _T("Company 2"); if(CeSeekDatabase(hDB, CEDB_SEEK_VALUEFIRSTEQUAL, (DWORD)&propSeek, &dwIndex)) { do { cout _T("Record index: ") dwIndex endl; ReadOneProp(hDB); } while (CeSeekDatabase(hDB, CEDB_SEEK_VALUENEXTEQUAL, (DWORD)&propSeek, &dwIndex)); } CloseHandle(hDB); } else cout _T("Could not open database") endl; } The success of the do/while loop depends on how the database is first opened.
Once the database is open, Listing 4.11 initializes propSeek with the property id (propCompany) and the value to search for ("Company 2"). The first call to CeSeekDatabase locates the first record that matches, then CeSeekDatabase is called in a do/while loop to locate the remaining records.
|