Step 2: Connect to a database

<< Click to Display Table of Contents >>

Navigation:  Application development > Introduction to BDE programming > BDE Programming in Delphi > Basic procedure >

Step 2: Connect to a database

Previous pageReturn to chapter overviewNext page

Now you are ready to connect to a database. All table access must be performed in the context of a database. Use the DbiOpenDatabase function to open the database. This functions stores a pointer to the database into the variable hDB. This variable will be used in later steps to specify the table’s directory and to open the table in the context of the database represented by hDB.

The preferred method is to create an alias and use that as the database. This permits easy future modification if one day it is decided to move the application from using dBASE tables to using InterBase tables.

Local databases generally use what is referred to as the "STANDARD" database, which is used in this example.

var

 hDB: hDBIDb;      // Database handle

begin

 …

 Check(DbiOpenDatabase(

  nil,            // Database name (nil for standard Database)

  nil,            // Database type (nil for standard Database)

   dbiREADONLY,    // Open mode (read-write or read-only)

   dbiOPENSHARED,  // Share mode (shared or exclusive)

  nil,            // Password (not needed for the STANDARD database)

  nil,            // Number of optional parameters

  nil,            // Field desc for optional parameters

  nil,            // Values for the optional parameters

   hDb)));          // Handle to the database

 …

end;