You use several functions to work with database records. These functions allow you to create, modify, and delete records and their properties.
You can create new records or modify existing records using the CeWriteRecordProps function. The function parameters include the handle to the database and the object identifier of the record to add. If the object identifier is zero, CeWriteRecordProps creates a new record.
To write properties to a record, fill an array of CEPROPVAL structures and pass the address of the array to CeWriteRecordProps along with the database handle and the record's object identifier. Each structure contains a property identifier and the data value for that property. To specify the data value, fill the val member, which is defined as a CEVALUNION union. The CEPROPVAL structure also includes a flag member that you can set to CEDB_PROPDELETE in order to delete the specified property or properties. If the CeWriteRecordProps function succeeds, the object identifier of the new or modified record is returned.
Use the CeDeleteRecord function to delete a record from a database, supplying the object identifier of the record and the handle to the open database that contains the record.
The following code example creates and writes a new property that is a byte array, or BLOB.
CEPROPVAL NewProp; // the new property contains a BLOB
CEBLOB blob; // the BLOB contains a byte array
BYTE * pBuf = NULL; // the actual BLOB data
UINT cbBuf; // count of bytes needed in BLOB
...
// figure out the size needed, then allocate it
pBuf = (BYTE *) LocalAlloc(LMEM_FIXED, cbBuf);
// put the actual data into pBuf here...
// now set up to write the new BLOB property
NewProp.propid = CEVT_BLOB;
NewProp.wFlags = 0;
blob.dwCount = cbBuf; // count of bytes in the buffer
blob.lpb = pBuf; // set CEBLOB field to point to buffer
NewProp.val.blob = blob; // BLOB itself points to the buffer
oid = CeWriteRecordProps(hDb,
0, // new record
1, // one property
&NewProp); // pointer to the BLOB property
// perform error handling by checking oid...
Write a record into a database by filling an array of CEPROPVAL structures and passing the array to the CeWriteRecordProps function, along with an open handle to the database in which to add the record. The following code example shows how to add a record to a database.
// SetAddressData - Adds a name and address to an address database in
// the object store.
// Returns the object identifier of the record in which the name and
// address are written.
// pAddressData - Pointer to a structure that contains the name and
// address to add
// Global variable:
// g_hAddressDatabase - Open handle to the address database
CEOID SetAddressData(PADDRESSDATA pAddressData)
{
CEPROPVAL rgPropVal[ADDRESS_PROP_COUNT];
WORD wCurrent = 0;
// Use a C runtime function to zero-fill the array of property
// values.
memset(&rgPropVal, 0, sizeof(CEPROPVAL) * ADDRESS_PROP_COUNT);
rgPropVal[wCurrent].propid = HHPR_NAME;
rgPropVal[wCurrent++].val.lpwstr = pAddressData->pwszName;
rgPropVal[wCurrent].propid = HHPR_STREET;
rgPropVal[wCurrent++].val.lpwstr = pAddressData->pwszStreet;
rgPropVal[wCurrent].propid = HHPR_CITY;
rgPropVal[wCurrent++].val.lpwstr = pAddressData->pwszCity;
rgPropVal[wCurrent].propid = HHPR_STATE;
rgPropVal[wCurrent++].val.lpwstr = pAddressData->pwszState;
rgPropVal[wCurrent].propid = HHPR_ZIP_CODE;
rgPropVal[wCurrent++].val.ulVal = pAddressData->dwZip;
oid = CeWriteRecordProps(g_hAddressDatabase, 0, wCurrent,
rgPropVal);
return oid;
}