Opening a database

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Opening a database

Return to chapter overview

A database must be opened with a call to DbiOpenDatabase before a table in the database can be opened. A successful call to DbiOpenDatabase returns the database handle, which is then passed in subsequent calls to many other BDE functions.

For SQL databases, a password and user name must be supplied with DbiOpenDatabase to connect to the server.

Specifying a standard database

The following code sample opens a standard database (used to access Paradox, dBASE, FoxPro, Access, and Text tables) by using a NULL database name and database type:

rslt = DbiOpenDatabase(NULL, NULL, dbiREADWRITE, dbiOPENSHARED, NULL, 0, NULL, NULL, &hDb)

To change the current directory for a standard database, call DbiSetDirectory

rslt = DbiSetDirectory(hDb, "C:\\DATE");

Specifying a SQL database

There are several different methods of specifying a SQL database in the DbiOpenDatabase call:

_bm0        The database name can specify a SQL alias, which defines a SQL database in the configuration file. If a SQL alias is specified, the database type is NULL and optional fields are not required.
_bm0        The database name can be NULL if the database type specifies one of the SQL driver names (for example, InterBase or Oracle). If optional parameters are not specified, driver-specific defaults are used.

For example, this code sample opens a named database on a SQL server:

rslt = DbiOpenDatabase("myalias", NULL, dbiREADWRITE, dbiOPENSHARED, "mypassword", 0, NULL, NULL, &hDb)

Specifying an alias

When calling DbiOpenDatabase you can supply an alias referencing a database name in the configuration file.

Specifying access rights

The eOpenMode and eShareMode parameters of the DbiOpenDatabase call, in combination with eOpenMode and eShareMode parameters of the DbiOpenTable call, determine the access rights of users to tables within a database.

Note:        For SQL data sources, the OPEN MODE parameter for each alias in the BDE configuration file takes precedence over the open mode parameters passed with DbiOpenDatabase.

If the database open mode is read-only, tables within that database cannot be opened by DbiOpenTable in read-write mode. If the database open mode is read-write, tables within that database can be opened by DbiOpenTable either in read-only or read-write mode.

If the database share mode is exclusive, tables within that database cannot be opened by DbiOpenTable in share mode. If the database was opened in share mode, tables within that database can be opened by DbiOpenTable in either exclusive or share mode.

Specifying optional parameters

Optional database-specific parameters can be passed to the DbiOpenDatabase function. To retrieve a list and description of these optional parameters for a database, the application can call DbiOpenCfgInfoList, supplying the path of the database name in the configuration file. This function returns the handle to a virtual table listing optional parameters for this database system and default values for these parameters.

OptFields, pOptFldDesc and pOptParams are the optional parameters, but may actually be required, depending on which driver is being used, and whether enough information has been supplied with other parameters to specify the database. For more on these parameters, see DbiCreateTable

 

 Preparing to access a table%!AL(`preparingtoaccess')

 Accessing and updating tables%!AL(`accessingtables')