Database records are updated using the CeWriteRecordProps function. You need to provide the record's object identifier, and this is generally obtained by calling CeSeekDatabase. Listing 4.14 opens the company database using the CompanyID as the sort order. It then locates the first record with a CompanyID of 2, and then updates the telephone number for that record.
Listing 4.14 Locates and updates a record
void Listing4_14() { HANDLE hDB; CEGUID ceObjStore; DWORD dwIndex; CEOID ceOidDB = 0; CREATE_SYSTEMGUID(&ceObjStore); hDB = CeOpenDatabaseEx(&ceObjStore, &ceOidDB, _T("Company"), propCompanyID, // prop.id. of sort order 0, // no auto-increment NULL); // no notifications if(hDB != INVALID_HANDLE_VALUE) { CEPROPVAL propSeek, propUpdate; CEOID ceOidRec = 0; propSeek.propid = propCompanyID; propSeek.val.lVal = 2; // company id to seek ceOidRec = CeSeekDatabase(hDB, CEDB_SEEK_VALUEFIRSTEQUAL, (DWORD)&propSeek, &dwIndex); if(ceOidRec != 0) { propUpdate.propid = propCompanyTel; propUpdate.wFlags = 0; propUpdate.val.lpwstr = _T("444-99988"); if(CeWriteRecordProps(hDB, ceOidRec, // object id to update 1, // number of properties &propUpdate)) cout _T("Record updated") endl; else cout _T("Record not updated") endl; } else cout _T("Could not locate company id 2") endl; CloseHandle(hDB); } else cout _T("Could not open database") endl; }
In this case a single property is updated in the record. You can pass in an array of CEPROPVAL structures if more than one property is to be updated. This is more efficient than calling CeWriteRecordProps many times.