Download the source code

Windows CE supports CEDB and EDB databases. CEDB (Property Database) is lightweight database API for Windows CE. CEDB provides storage, access and sorting records. The new EDB replaces CEDB database. The CEDB is considered as obsolete for Windows Mobile 6 onwards. EDB volumes can contain multiple databases.

An EDB database contains a number of records. Records are a collection of properties. A property in Windows CE Database can have be one of the Windows CE supported data type.

To open or create a database, we should mount the volume. A volume is a file which is specially and can contain CE database. Before using the database we have to mount the volume and after the use we should unmount the volume. After mounting it will be assigned a unique GUID and is represented as CEGUID structure. It should be noted that mounting is reference counted, each time the volume is mounted reference count is incremented and when unmounted the reference is decremented and when the count is zero it will be closed. A volume can contain multiple databases. Once a volume is mounted, we can open any database that is the volume.

To mount a volume we can use the API CeMountDBVolEx API. Syntax is defined as:

BOOL CeMountDBVolEx(PCEGUID pGuid,LPWSTR lpwszDBVol, CEVOLUMEOPTIONS* pOptions,DWORD dwFlags);

The parameter pGuid will receive the CEGUID of the mounted volume. The third parameter is a pointer to CEVOLUMEOPTIONS structure which is used to configure the database volume options such as buffer pool size, flush intervals, password, etc… After mounting the volume we create database session using the API CeCreateSession. After creating a session we can open an existing database using the API CeOpenDatabaseInSession or create a new database using the API CeCreateDatabaseWithProps. If we create the database we should still open the database in order to work.

After opening we can peform read or write operations. To read a record we can use the API CeReadRecordPropsEx. The syntax is defined as

CEOID CeReadRecordPropsEx(HANDLE hDbase,DWORD dwFlags,LPWORD, CEPROPID* prgPropID,LPBYTE* lplpBuffer,LPDWORD lpcbBuffer,HANDLE hHeap);

The dwFlags can be either 0 or CEDB_ALLOWREALLOC. The CEDB_ALLOWREALLOC flag tells the system to use the buffer specified by the lplpBuffer parameter is allocated using LocalAlloc and can reallocate if necessory. The record retrieved will be copied to the lplpBuffer. This will be an array of CEPROPVAL structure. This structure contains the data type and the data itself. The last parameter can be NULL or a handle of an application created heap. If this parameter is not NULL, the system will use this heap handle to reallocate the lplpBuffer. The CeReadRecordPropsEx returns a CEOID which represents the current row. This value is an identifier that represents the row fetched. We can use this value to seek to a particular row.

To write to the database we use the API CeWriteRecordProps and the syntax is defined as:

CEOID CeWriteRecordProps(HANDLE hDatabase, CEOID oidRecord,WORD PropID,CEPROPVAL* prgPropVal);

The past parameter prgPropVal is an array of CEPROPVAL structure which contains the actual data.

The CEPROPVAL structure is defined as:

typedef struct _CEPROPVAL{
CEPROPID propid;
WORD wLenData;
WORD wFlags;
CEVALUNION val;
} CEPROPVAL;
typedef CEPROPVAL *PCEPROPVAL;

The member propid is the identifier or data type of the property value. The higher order word can be an application defined value and low order word is one of the supported data type. The data type can be one of the folowing constants:

CEVT_BLOB
CEVT_BOOL
CEVT_FILETIME
CEVT_I2
CEVT_I4
CEVT_LPWSTR
CEVT_R8
CEVT_UI2
CEVT_UI4

These values are self explanatory. We can use a #define for a particular field such as

#define FIELD_TITLE MAKELONG(CEVT_LPWSTR,1)

The member val is a union of type CEVALUNION which defined as:

typedef union _CEVALUNION {
short iVal;
USHORT uiVal;
long lVal;
ULONG ulVal;
FILETIME filetime;
LPWSTR lpwstr;
CEBLOB blob;
BOOL boolVal;
double dblVal;
} CEVALUNION;

We have to set the values based on the data type of the corresponding property such as lpwstr for type, blob for CEVT_BLOB, etc…

The sample application presented here is a simple application to manage book details such as title, ISBN, authors, description etc… You can add, delete and modify the book details. The application is compiled using Visual Studio 2008 using Windows Mobile SDK 6.