Creating and Deleting Databases

The CeCreateDatabase function creates a database. When calling the function, specify the name, an optional database type identifier, and optional sort order specifications. CeCreateDatabase returns the object identifier of the newly created database.

The database type identifier is an optional, application-defined value that allows you to differentiate individual databases. For example, you can have a different type of database for an address book than for a to-do list. The type identifier allows you to group related databases for searching, record management, and enumeration of databases.

For a discussion of sorting and sort orders, see Sorting Records.

To delete a database, the application passes the database's object identifier to CeDeleteDatabase.

The following code example attempts to open a database of addresses by calling the CeOpenDatabase function. If the database does not exist, the example calls the CeCreateDatabase function to create a new address database with three different sort orders. After creating the database, the example tries again to open the database.

// Global variables:
//    g_oidAddressDatabase - Object identifier of address database
//    g_hAddressDatabase - Open handle to the address database

BOOL OpenAddressDatabase (HWND hwndNotify, CEPROPID cepidSortProperty)
{
   CEOID oidAddressDatabase;   // Object identifier of address database
   SORTORDERSPEC sort[MAX_MSG_PROPERTIES]; // Sort order descriptions

   g_hAddressDatabase = CeOpenDatabase(&oidAddressDatabase, 
      TEXT("Addresses"),  cepidSortProperty, 0, hwndNotify);

   if (g_hAddressDatabase == INVALID_HANDLE_VALUE) 
   { 

      sort[0].propid = HHPR_LAST_NAME;    
      sort[0].dwFlags = 0;                 // sort in ascending order
      sort[1].propid = HHPR_CITY;
      sort[1].dwFlags = 0;                 // sort in ascending order
      sort[2].propid = HHPR_STATE;
      sort[2].dwFlags = 0;                 // sort in ascending order

        g_oidDatabase = CeCreateDatabase(TEXT("Addresses"), 0, 
            MAX_MSG_PROPERTIES, sort);

        g_hAddressDatabase = CeOpenDatabase(&oidAddressDatabase, NULL, cepidSortProperty, 0, 
            NULL);
   }

   if (!g_hAddressDatabase)
      return FALSE;

   return TRUE;
}