Primary Property Example (VC++)

In the Biblio.mdb database, this example creates a new Table and primary key Index object and then adds the Table to the TableDefs collection of a Database.

CdbDBEngine      dbeng;
CdbDatabase      dbsBiblio;
CdbIndex      idxPrimaryKey;
CdbField      fldTypeNum, fldType;
CdbTableDef      tdfBookTypes;

//Open the Database.
dbsBiblio = dbeng.OpenDatabase(__T("Northwind.mdb"));
tdfBookTypes = dbsBiblio.CreateTableDef(_T("Book Types"));

// Create and Set fldTypeNum field 
fldTypeNum = tdfBookTypes.CreateField(_T("TypeNum"), dbLong);
// Add fldTypeNum field to TableDef.
tdfBookTypes.Fields.Append(fldTypeNum);

// Create and Set fldType field properties.
fldType = tdfBookTypes.CreateField(_T("Type"),dbText);

// Set Size property of text field.
fldType.SetSize(15);   

// Add fldType field to TableDef.
tdfBookTypes.Fields.Append(fldType);

// Set properties in new Index.
idxPrimaryKey = tdfBookTypes.CreateIndex(_T("PrimaryKey"));
idxPrimaryKey.SetName(_T("PrimaryKey"));
idxPrimaryKey.SetUnique(TRUE);
idxPrimaryKey.SetPrimary(TRUE);

// put the TypeNum field in this index
idxPrimaryKey.Fields.Append((CdbField)idxPrimaryKey.CreateField(
_T("TypeNum")));

// Add Index to TableDefs collection.
tdfBookTypes.Indexes.Append(idxPrimaryKey);

//   Append TableDef to database.
dbsBiblio.TableDefs.Append(tdfBookTypes);
dbsBiblio.Close();