Using the Microsoft Jet Workspace

Opening a Database

To open a database, you simply open an existing CdbDatabase object or create a new one. This object can represent a Microsoft Jet database (.mdb file), an ISAM database (for example, Paradox), or an ODBC database connected through the Microsoft Jet database engine (also known as a "Microsoft Jet-connected ODBC database").

Data-Definition Language

You can use object variables and other DDL features to modify your database structure. For example, you can add a new CdbField object to an existing table with the following code:

CdbDBEngine   eng;
CdbDatabase   dbs;
CdbTableDef   tdf;
CdbField      fld;
// Open a database.
dbs = eng.OpenDatabase("Biblio.mdb");
// Open a TableDef.
tdf = dbs.TableDefs["Authors"];
// Create a new field.
fld = tdf.CreateField("Address", dbText, 20);
// Append field to the TableDef Fields collection.
tdf.Fields.Append(fld);

This code creates a new object variable for a CdbField object and adds it to a CdbTableDef object with the Append method. Because a CdbTableDef object contains the definition of a table, the table now has a field named Address for entering data. In much the same way, you can create new tables and new indexes.

Data Manipulation

DAO provides an excellent set of data manipulation tools. You can create a CdbRecordset object to conveniently query a database and manipulate the resulting set of records. The OpenRecordset method accepts an SQL string or a CdbQueryDef (stored query) name as a data source argument, or it can be opened from a CdbQueryDef object or a CdbTableDef object, using that object as its data source. The resulting CdbRecordset object features an extremely rich set of properties and methods with which to browse and modify data.

The CdbRecordset object is available in four different types — Table, Dynaset, Forward-Only, and Snapshot.

Transactions

All CdbDatabase objects opened against a CdbWorkspace object share a common transaction scope. That is, when you use the BeginTrans method on a CdbWorkspace object, it applies to all open databases within that CdbWorkspace object. In the same way, when you use the CommitTrans method against the CdbWorkspace, it applies to all open databases in the CdbWorkspace object.

Replication

You can use database replication to create and maintain replicas of a master Microsoft Jet database, using the Synchronize method to periodically update all or part of the replicas, or to copy new data from one replica to another. You can also restrict the update to only selected records, using the ReplicaFilter property, and then synchronize those records with the PopulatePartial method.

Security

You can restrict access to one or more .mdb databases or their tables using security settings established and managed by the Microsoft Jet database engine. In your code, you can establish CdbGroup and CdbUser objects to define the scope and level of permissions available to individual users on an object-by-object basis. For example, you can establish permissions for a specific user to provide read-only access to one table and full access to another.