Table of Contents

22.4.2 Class Connections (gb.db)

The class lists all predefined DB connections that are defined in the IDE in the “Connections” project folder. You must include the gb.desktop component in your project if you want to ensure that the password entered via the IDE for the defined DB connection is automatically retrieved. Otherwise, you must provide the password for MySQL and PostgreSQL yourself!

22.4.2.1 Properties

The Connections class has two properties:

PropertyData typeDescription
CountIntegerDelivers the number of DB connections defined in the project.
KeyStringDelivers the key of the last enumerated predefined DB connection.

Table 22.4.2.1.1 : Properties of the Connections class

A predefined DB connection is one that has been defined in the IDE via the context menu> Connections> New connection in the project browser. This creates a text file in the hidden.connection directory of the project that can be accessed by the runtime environment.

22.4.2.2 Methods

The Connections class has these two methods:

MethodReturn typeDescription
Create(Name As String) ConnectionReturns a DB connection object defined in the IDE and initialises it. `Name` is the name of the DB connection defined in the IDE. The database defined by the DB connection object is created if it does not exist. The database tables and database indices are created and initialised from the internal template description of the DB connection.
Exist(Name As String)BooleanReturns TRUE if a specific predefined connection exists. `Name` is the name of the DB connection defined in the IDE.

Table 22.4.2.2.1 : Methods of the Connections class

22.4.2.3 Examples

The following source code provides an enumeration of all predefined DB connection objects:

Dim hConnection As Connection
For Each hConnection In Connections
  ...
Next 

This source code section provides a predefined DB connection by name:

Dim hOneConnection As Connection
hOneConnection = Connections [ Name As String ]

This opens a predefined DB connection and closes it at the end of the programme:

Public Sub Form_Open()
  If Not Connections["Connection1"].Opened Then Try Connections["Connection1"].Open()
  If Error Then
    Message.Error("Unable to connect to the database")
    Me.Close()
  Endif
End
 
Public Sub Form_Close()
  Connections["Connection1"].Close()
End