Establishing database connections
The following examples show several methods for establishing a database connection.
•Using a connection specification
•Using a connection alias
•Using a logon specification
•Using a connection specification and connection alias with SQLite
"Connect to a database using a connection specification.
This example prompts you with a database logon window.
If you are already connected, this code just returns the
active connection."
| conSpec |
conSpec := AbtDatabaseConnectionSpec
forDbmClass: #AbtIbmCliDatabaseManager
databaseName: 'SAMPLE'.
conSpec connect.
"Connect to a database and assign an alias to the connection"
| conSpec |
conSpec := AbtDatabaseConnectionSpec
forDbmClass: #AbtIbmCliDatabaseManager
databaseName: 'SAMPLE'.
conSpec connectUsingAlias: 'SampleConSpec'.
"Connect to a database using a connection specification
and a logon specification. This example does not prompt
you with a database logon window."
| conSpec logonSpec |
conSpec := AbtDatabaseConnectionSpec
forDbmClass: #AbtIbmCliDatabaseManager
databaseName: 'SAMPLE'.
logonSpec := AbtDatabaseLogonSpec
id: 'userid'
password: 'password'
server: nil.
conSpec connectUsingAlias: 'SampleConSpec'
logonSpec: logonSpec.
"Disconnect from a database using a connection specification.
Return the connection."
| conSpec activeConnection|
conSpec := AbtDatabaseConnectionSpec
forDbmClass: #AbtIbmCliDatabaseManager
databaseName: 'SAMPLE'.
activeConnection := conSpec connect.
activeConnection disconnect;
yourself.
"SQLite: Connect to a file-based database named Test.db in the current working directory"
| conSpec conn|
conSpec := AbtDatabaseConnectionSpec
forDbmClass: #AbtSQLiteDatabaseManager
databaseName: 'Test.db'.
conn := conSpec connect.
conn disconnect.
"SQLite: Connect to an in-memory database"
| conSpec conn |
conSpec := AbtDatabaseConnectionSpec
forDbmClass: #AbtSQLiteDatabaseManager
databaseName: ':memory:'.
conn := conSpec connect.
conn disconnect.
"SQLite: Connect to a file-based database and assign an alias to the connection"
| conSpec conn |
conSpec := AbtDatabaseConnectionSpec
forDbmClass: #AbtSQLiteDatabaseManager
databaseName: 'Test.db'.
conn := conSpec connectUsingAlias: 'TestConSpec'.
conn disconnect.
Last modified date: 01/29/2015