Recordset Object, Recordsets Collection Example (VC++)

This example creates a new Recordset object and opens it (thereby appending it to the Recordsets collection) in the default database. Then the example enumerates all the Recordset objects in the current database and all the fields in each Recordset object and closes the new Recordset. See the methods and properties in the Recordset summary topic for additional examples.

CdbDBEngine      dbeng;
CdbDatabase      db;
CdbRecordset   rstOrders, rstTemp;
int            nRS, nFld;

// Open the database.
db = dbeng.OpenDatabase(_T("Northwind.mdb"));

// Open a Recordset.
rstOrders = db.OpenRecordset("Orders", dbOpenSnapshot);

// Enumerate Recordsets.
for (nRS = 0; nRS < db.Recordsets.GetCount(); nRS++)
   {
   rstTemp = db.Recordsets[nRS];
   printf("Recordset object(%d): \n", nRS, rstTemp.GetName());
   
   // Enumerate fields.
   printf("Fields: Name, Type, Value\n");
   for (nFld = 0; nFld < rstTemp.Fields.GetCount(); nFld++)
      {
      printf("  %s, %s", rstTemp.Fields[nFld].GetName(),
      rstTemp.Fields[nFld].GetType());
      if (rstTemp.Fields[nFld].GetType() == dbText)
         printf(", %s\n", rstTemp.Fields[nFld].GetValue());
      else
         printf("\n");
      }
   }
      
db.Close();