Opening and Closing Property Databases

< BACK  NEXT >
[oR]

Property databases are opened using the function CeOpenDatabaseEx. This function returns a HANDLE to the open database, which must eventually be closed by calling CloseHandle. Listing 4.4 shows how to open the "Company" database created in the previous section.

Listing 4.4 Opens a database
 HANDLE Listing4_4() {   CEGUID ceObjStore;   HANDLE hDB;   CEOID ceOidDB = 0;   CREATE_SYSTEMGUID(&ceObjStore);   hDB = CeOpenDatabaseEx(&ceObjStore,        &ceOidDB,        _T("Company"),        propCompany,      // prop.id. of sort order        CEDB_AUTOINCREMENT,        NULL);            // no notifications   if(hDB == INVALID_HANDLE_VALUE)      cout   _T("Could not open database")   endl;   else      cout   _T("Database Opened")   endl;   return hDB; } 

The ceObjStore variable is initialized to reference the ObjectStore using the CREATE_SYSTEMGUID macro, and this is passed to CeOpenDatabaseEx function. You can specify the database to open either through its object id (in ceOidDB in Listing 4.4), or by name. You should initialize the object id variable to zero if the database's name is used.

You should specify which sort order to use when opening the database you cannot change the sort order being used without first closing and then reopening the database. In this case, propCompany is used.

The CEDB_AUTOINCREMENT flag specifies that the current record pointer will be updated to refer to the next record after the current record has been read. The final parameter (passed as NULL in Listing 4.4) is used to allow the application to receive notifications through Windows messages when other applications modify records in the database. The function returns a HANDLE to the open database (which in this case is returned to the caller), or INVALID_HANDLE_VALUE on failure.

An open database should be closed by passing the HANDLE returned from CeOpenDatabaseEx to the CloseHandle function, Listing 4.5.

Table 4.9. CeOpenDatabaseEx Opens an existing database
CeOpenDatabaseEx
PCEGUID pceguid Mounted Database Volume or Object Store CEGUID.
PCEOID poid Pointer to the database's Object ID. 0 if lpszName is specified.
LPWSTR lpszName Name of database to open. NULL if poid is specified.
CEPROPID propid Property ID of sort order to use on opened index.
DWORD dwFlags 0, or CEDB_AUTOINCREMENT for automatic moving to next record.
CENOTIFYREQUEST *pRequest hwndNotify Pointer to CENOTIFICATION structure, or NULL for no notification.
HANDLE Return Value Returns HANDLE to open database, or INVALID_HANDLE_VALUE on failure.

Listing 4.5 Closes a database
 // *** Listing 4.5 // // Closes database specified by object identifier void Listing4_5(HANDLE hDB) {   if(!CloseHandle(hDB))      cout   _T("Could not close database");   else      cout   _T("Database closed"); } 

You will need to reopen the database if you want to select another sort order. When you open a database, store the object id of the database, and use this value to reopen the database. Using the object id is more efficient than using the database's name.

   hDB = CeOpenDatabaseEx(&ceObjStore,     &ceOidDB,    // returned from prev. database open     NULL,        // NULL database name     propCompany,     CEDB_AUTOINCREMENT,     NULL); 

< 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