dbOpenConnection()

Database function for opening a connection to a data source.

Synopsis

int dbOpenConnection(string connectStr, dbConnection &connection);

Parameters

Parameter Meaning
connectStr String with connection information on the data source
connection Reference to the connection for use in subsequent function calls (return parameter)

Parameters that are returned (i.e. can be changed by the function) are labelled with "&" (& is the reference operator).

Return Value

The return value is an error code; 0 indicates the operation has been performed successfully. Note that no error message is shown under Windows. For other values dbGetError() can, however, be used to obtain detailed error information.

Errors

Appropriate error codes are returned for invalid connection data or incomplete ADO installation. In the event of an error, the connection is not valid and cannot be released (dbCloseConnection()).

Description

A connection object is necessary in order to be able to access data sources using the DB interface. This object is created with the dbOpenConnection() function, where an appropriate connect string identifying the data source must be specified.

Under Windows and Linux the connect string can be specified in two ways:

Option 1: A direct connection via separate parameters:

"driver=<name>;server=<name>;uid=<name>;pwd=<name>;database=<name>;port=<nnnn>;"

The parameters uid, pwd and port are optional. Note that you have to pass a number at the parameter port otherwise an appropriate error message is displayed.

The following values for the parameter driver are supported under Linux:

  • "QMYSQL" - MySQL 4.x or higher

  • "QOCI8" - Oracle Call Interface (OCI), Oracle 8.x and Oracle 9.x

  • "QODBC3" - Open Database Connectivity (ODBC)

  • "QPSQL7" - PostgreSQL Database

Option 2: A connection via Data Source Name (DSN) with ODBC-Tags. The syntax must look as follows (UID and PWD are optional):

"DSN=<name>;[UID=<name>;[PWD=<name>;]]"

Under Windows you have another possibility for specifying the connect string:

Option 3: Via Data Source Name (DSN) with OLE DB-Tags

"Data Source=Pubs;User ID=sa;Password=pwd;"

DSN names can be created and managed with the ODBC Data Source Administrator for instance. The ORACLE client software must be installed for accessing ORACLE databases.

Example of a connect string for MS Access under Windows:

"DRIVER=Microsoft Access Driver (*.mdb);DBQ=TEST.MDB;DefaultDir=C://test//jetdb;UID=;PWD=;"

Example of a connect string for Oracle under Windows:

"DSN=ORA1;UID=SCOTT;PWD=tiger;"

Example of a connect string for MYSQL under Linux:

"driver=QMYSQL3;database=test;server=localhost;"

Example of a connect string for Oracle under Linux:

"driver=QOCI8;server=K-WinCC_OA ORACLE0;uid=WinCC OA;pwd=wincc_oa;database=WinCC_OA;port=1521;"

Example

See the example of the function dbOpenRecordset().

Assignment

ADO and Qt, Database functions

Availability

CTRL