Manipulating Data Records in Win CE

We are going to continue in our exploration of CE native database functionality by learning how to manipulate specific individual records. To this end, we'll be looking at a bit more of the code from "BetterBirthdays," the example program provided with the last lesson.

Deleting a Record from the Database

To begin, let's examine the ways in which we can use a record's CEOID, which is returned by a successful call to CeSeekDatabase(). (Note that in the example code, we saved this value in the global variable globalRecOID.)

Deleting a record is a straightforward operation, so we will start there. Below is a fragment of code from the message switch of the BirthdayDlgProc() function:

To delete a record, we use athe simplest form of the CeOpenDatabase() function, identifying the database with the previously saved CEOID, globalCEOID, and setting all remaining parameters to zero. Using the handle to the database returned by CeOpenDatabase(), and the record CEOID saved after the prior call to FindBirthday(), we delete the record with a call to FindBirthday(), we delete the record with a call to CeDeleteRecord(), and then close the database.

Modifying an Existing Record

We also can access a record by CEOID in order to make modifications. Implementing this capability requires only a few enhancements to the AddBirthdayReminder() function. We used AddBirthdayReminder() in the first example program, BirthdayReminder. Because this new version of the function is similar in many ways to the original version, we'll just highlight the changes here.

First, notice that we have added an item to the original function's parameter list. The new parameter, an int, is used as a flag. It can assume one of two values: ADD_REC or UPDATE_REC. The value of this flag determines whether the function adds a new record or updates an existing record.

BOOL AddBirthdayReminder( HWND hDlg,
int iAddUpdateFlag ){

Another change is that in this version of the program, we no longer store the birth date as the SYSTEMTIME structure that is returned by the dialog's Date Picker control:

Rather, we convert the SYSTEMTIME value to a FILETIME. The FILETIME format is much more efficient, for two reasons. First, it encodes all the information in the SYSTEMTIME structure in a single DWORD, saving a great deal of storage space. Second, the CE native database type CEVT_FILETIME directly supports FILETIME values, so its translated value can be stored directly in the CEVALUNION member val.filetime. Here's the new CEPROPVAL structure initialization for this property:

When we write the record to the database, one of two circumstances will prevail: Either we will be creating a new record at the end of the database, or we will be updating an existing record that we can precisely and immediately locate by CEOID. For this reason, we can do a simple database open operation: