Database Object, Databases Collection Example (VC++)

This example creates a new Database object and opens it (thereby appending it to the Databases collection) in the default Workspace object. Then it enumerates all the collections contained by each Database object and the properties of the new Database object and closes the new Database. See the methods and properties listed in the Database summary topic for additional examples.

CdbDBEngine         dbeng;
CdbWorkspace      wrkDefault;
CdbDatabase         dbsEnum, dbsTemp;
long            intOBJ, intDB;

wrkDefault = dbeng.Workspaces[0L];

dbsEnum = wrkDefault.CreateDatabase(_T("Northwind.mdb"), dbLangGeneral);
// Enumerate all open databases.
for (intDB = 0; wrkDefault.Databases.GetCount(); intDB++)
   {
   dbsTemp = wrkDefault.Databases[intDB];
   printf(_T("Enumeration of Database: %s\n"), dbsTemp.GetName());
   //   Enumerate containers.
   printf(_T("Container: Name, Owner"));
   for (intOBJ = 0; intOBJ < dbsTemp.Containers.GetCount(); intOBJ++)
      printf(_T("  %s, %s\n"), dbsTemp.Containers[intOBJ].GetName(), 
            dbsTemp.Containers[intOBJ].GetOwner());
      
   // Enumerate query definitions.
   printf(_T("QueryDef: Name"));
   for (intOBJ = 0; intOBJ < dbsTemp.QueryDefs.GetCount(); intOBJ++)
      printf(_T("  %s\n"), dbsTemp.QueryDefs[intOBJ].GetName());
   
   // Enumerate Recordsets.
   // No output because no Recordset is open.
   printf(_T("\nRecordset: Name"));
   for (intOBJ = 0; intOBJ < dbsTemp.Recordsets.GetCount(); intOBJ++)
      printf(_T("  %s\n"), dbsTemp.Recordsets[intOBJ].GetName());
   
   // Enumerate relationships.
   printf(_T("\nRelation: Name, Table, ForeignTable"));
   for (intOBJ = 0; intOBJ < dbsTemp.Relations.GetCount(); intOBJ++)
      printf(_T("  %s, %s, %s\n"), dbsTemp.Relations[intOBJ].GetName(),
                  dbsTemp.Relations[intOBJ].GetTable(),
                  dbsTemp.Relations[intOBJ].GetForeignTable());
   
   // Enumerate table definitions.
   printf(_T("\nTableDef: Name, DateCreated"));
   for (intOBJ = 0; intOBJ < dbsTemp.TableDefs.GetCount(); intOBJ++)
      printf(_T(" %s, %s\n"), dbsTemp.TableDefs[intOBJ].GetName(),
                     dbsTemp.TableDefs[intOBJ].GetDateCreated());
      
   
   // Enumerate built-in properties of dbsEnum.
   printf(_T(" dbsEnum.Name: %s\n"), dbsEnum.GetName());
   printf(_T(" dbsEnum.CollatingOrder: %s\n"), 
                        dbsEnum.GetCollatingOrder());
   printf(_T(" dbsEnum.Connect: %s\n"), dbsEnum.GetConnect());
   printf(_T(" dbsEnum.QueryTimeout: %s\n"), 
                        dbsEnum.GetQueryTimeout());
   printf(_T(" dbsEnum.Transactions: %s\n"), 
                        dbsEnum.GetTransactions());
   printf(_T(" dbsEnum.Updatable: %s\b"), dbsEnum.GetUpdatable());
   
   dbsEnum.Close;   // File remains on disk.
   }