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 databaseHANDLE 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.
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);
|