Searching for Records

< BACK  NEXT >
[oR]

The function CeSeekDatabase can be used to:

  • Move to the first or last record in a property database

  • Move a given number of records forward or backward from the current record

  • Move to a record with the given record object id

  • Move to records based on the current sort order

Once the desired record has been located, CeReadRecordPropsEx can be used to read properties from the record.

Table 4.13. CeSeekDatabase Moves to a different record in the property database
CeSeekDatabase
HANDLE hDatabase HANDLE to open database
DWORD dwSeekType Constant specifying how to seek (see below)
DWORD dwValue Value used when seeking using sort order or relative record position
LPDWORD lpdwIndex Record number to which seek moved
CEOID Return Value Returns object identifier of database record, or 0 if record not found

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

Table 4.14. Seek constants
Constant Purpose
CEDB_SEEK_BEGINNING Seek dwValue records from the start of the database.
CEDB_SEEK_END Seek dwValue records from the end of the database.
CEDB_SEEK_CURRENT Seek dwValue records from the current record. dwValue can be positive or negative, but the value should be cast to a DWORD.
CEDB_SEEK_CEOID Seek the record whose object identifier is specified in dwValue. This is very efficient.

Listing 4.10 shows a call to CeReadRecordPropsEx to locate the last record in the database.

Listing 4.10 Locates last record in database
 void 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.

Table 4.15. SeekType Constants
Constant Purpose
CEDB_SEEK_VALUEFIRSTEQUAL Locates the first record with the specified value
CEDB_SEEK_VALUENEXTEQUAL Locates the next record with the specified value from the current record
CEDB_SEEK_VALUESMALLER Finds the next record with the largest value that is smaller than the specified value
CEDB_SEEK_VALUEGREATER Finds the next record with a value greater than or equal to the specified value

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 value
 void 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.

  • It is essential that you open the database specifying the same sort order as you use in CeSeekDatabase (propCompany in this case).

  • Do not use CEDB_AUTOINCREMENT when opening the database, since reading records with CeReadRecordPropsEx will skip over records that CeSeekDatabase would otherwise locate.

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.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net