This example creates a new TableDef object and two new Field objects, appends the Field objects to the Fields collection in the new TableDef, and appends the TableDef to the TableDefs collection in the database. Then it creates a new primary Index object, includes the two Field objects in it, and appends the Index to the Indexes collection of the TableDef. Finally, the example enumerates the Index objects in the current database. See the methods and properties listed in the Index summary topic for additional examples.
CdbDBEngine dbeng;
CdbDatabase dbsDefault;
CdbTableDef tdfTest;
CdbField fldOne, fldTwo;
CdbIndex idxPrimary;
long I;
// Get workspace and database.
dbsDefault = dbeng.OpenDatabase(_T("Northwind.mdb"));
// Create table with two fields.
tdfTest = dbsDefault.CreateTableDef(_T("MyTable"));
fldOne = tdfTest.CreateField(_T("Field1"), dbLong);
fldOne.SetRequired(TRUE); // No Null values allowed.
tdfTest.Fields.Append(fldOne);
fldTwo = tdfTest.CreateField(_T("Field2"), dbLong);
fldTwo.SetRequired(TRUE); // No Null values allowed.
tdfTest.Fields.Append(fldTwo);
dbsDefault.TableDefs.Append(tdfTest);
// Create primary index for those two fields.
idxPrimary = tdfTest.CreateIndex(_T("MyIndex"));
idxPrimary.SetPrimary(TRUE);
fldOne = tdfTest.CreateField(_T("Field1"));
idxPrimary.Fields.Append(fldOne);
fldTwo = tdfTest.CreateField(_T("Field2"));
idxPrimary.Fields.Append(fldTwo);
tdfTest.Indexes.Append(idxPrimary);
// Enumerate index and its fields.
printf(_T("Index: %s\n"), idxPrimary.GetName());
printf(_T(" Required: %d\n"), idxPrimary.GetRequired());
printf(_T(" IgnoreNulls: %d\n"), idxPrimary.GetIgnoreNulls());
printf(_T(" Primary: %d\n"), idxPrimary.GetPrimary());
printf(_T(" Clustered: %d\n"), idxPrimary.GetClustered());
printf(_T(" Unique: %d\n"), idxPrimary.GetUnique());
printf(_T(" Foreign: %d\n"), idxPrimary.GetForeign());
printf(_T("\nFields in Index:\n"));
for( I = 0; I < idxPrimary.Fields.GetCount(); I++)
printf(_T(" %s\n"), idxPrimary.Fields[I].GetName());
This example opens a table-type Recordset and selects an index for the Recordset. By setting an index, the Microsoft Jet database engine returns records in the order specified by the index. Without an index, table-type Recordset objects return records from the database table in no particular order.
CdbDBEngine dbeng;
CdbDatabase dbsDefault;
CdbRecordset rstTitles;
// Get workspace and database.
dbsDefault = dbeng.OpenDatabase(_T("Northwind.mdb"));
rstTitles = dbsDefault.OpenRecordset(_T("Titles"));
rstTitles.SetIndex(_T("MyIndex"));
.
.
.