Database.Connect

From Xojo Documentation

Method

Database.Connect()

New in 2019r2

Supported for all project types and targets.

Connects to the database so that you can begin using it. If the connection cannot be made, a DatabaseException is raised.

Sample Code

SQLite

Var dbFile As FolderItem
dbFile = Folderitem.ShowOpenFileDialog("")

If dbFile <> Nil Then
Var db As New SQLiteDatabase
db.DatabaseFile = dbFile
Try
db.Connect
MessageBox("Connected to " + dbFile.Name)
Catch error As DatabaseException
MessageBox("Error: " + error.Message)
End Try
End If

PostgreSQL

Var db As New PostgreSQLDatabase

db.Host = "localhost"
db.UserName = "postgres"
db.Password = "dbexample"
db.DatabaseName = "postgres"

Try
db.Connect
MessageBox("Connected to PostgreSQL!")
Catch error As DatabaseException
MessageBox("Error connecting to PostgreSQL: " + error.Message)
End Try


Method

Database.Connect() As Boolean

Supported for all project types and targets.

Connects to the database so that you can begin using it. Before proceeding with database operations, test to be sure that Connect returns True.

Notes

If Connect returns False, you should also check the ErrorCode and ErrorMessage.

Sample Code

SQLite

Dim dbFile As FolderItem
dbFile = GetOpenFolderItem("")

If dbFile <> Nil Then
Dim db As New SQLiteDatabase
db.DatabaseFile = dbFile
If db.Connect Then
MsgBox("Connected to " + dbFile.Name)
Else
MsgBox("Error: " + db.ErrorMessage)
End If
End If

PostgreSQL

Dim db As New PostgreSQLDatabase

db.Host = "localhost"
db.UserName = "postgres"
db.Password = "dbexample"
db.DatabaseName = "postgres"

If db.Connect Then
MsgBox("Connected to PostgreSQL!")
Else
MsgBox("Error connecting to PostgreSQL: " + db.ErrorMessage)
End If