Database.Connect
From Xojo Documentation
Method
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
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
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
This item was deprecated in version 2019r2. Please use Database.Connect (above) as a replacement. |
Method
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
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