Class

PostgreSQLDatabase


Description

Used to connect to a PostgreSQL database.

Events

Name

Parameters

Returns

ReceivedNotification

Name As String, ID As Integer, Extra As String

Property descriptions


PostgreSQLDatabase.AppName

AppName As String

Use this to specify the name of the app that is connected to the PostgreSQL database.

When using PostgreSQL DB tools that show this name, setting this property makes it easier to know which apps owns the connection.

// db is an existing PostgreSQLDatabase connection
db.AppName = App.ExecutableFile.Name

PostgreSQLDatabase.DatabaseName

DatabaseName As String

The name of the database to open.

The DatabaseName is typically used with server databases (such as MySQL or PostgreSQL) to identify the specific database to use on the server.

This code connects to a PostgreSQL database called “BaseballLeague”:

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.Port = 5432
db.DatabaseName = "BaseballLeague"
db.UserName = "broberts"
db.Password = "streborb"
Try
  db.Connect
  // Use the database
Catch error As DatabaseException
  // DB Connection error
  MessageBox(error.Message)
End Try

PostgreSQLDatabase.Host

Host As String

The database host name or IP address of the database server.

This examples connects to a PostgreSQL database called “BaseballLeague”:

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.Port = 5432
db.DatabaseName = "BaseballLeague"
db.UserName = "broberts"
db.Password = "streborb"
Try
  db.Connect
  // Use the database
Catch error As DatabaseException
  // DB Connection error
  MessageBox(error.Message)
End Try

PostgreSQLDatabase.MultiThreaded

MultiThreaded As Boolean

If True, calls to SQLSelect and SQLExecute are threaded. The default is True.

When MultiThreaded is true, database access does not block other threads (or otherwise lock your running application).

However, performance is greatly improved if MultiThreaded = False

// db is an existing PostgreSQLDatabase connection
db.MultiThreaded = False

PostgreSQLDatabase.Password

Password As String

The password that is required for access to the database. Typically used in conjunction with UserName.

This examples connects to a PostgreSQL database called “BaseballLeague”:

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.Port = 5432
db.DatabaseName = "BaseballLeague"
db.UserName = "broberts"
db.Password = "streborb"
Try
  db.Connect
  // Use the database
Catch error As DatabaseException
  // DB Connection error
  MessageBox(error.Message)
End Try

PostgreSQLDatabase.Port

Port As Integer

The port of the PostgreSQL database to connect to. PostgreSQL’s default port is 5432.

This example opens an existing PostgreSQL database and changes the Port.

Var db As PostgreSQLDatabase
db = New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.port = 5516
db.DatabaseName = "myDatabase"
db.Username = "Charlie"
db.Password = "mashie"
Try
  db.Connect
  // proceed with database operations
Catch error As DatabaseException
  MessageBox("The connection failed: " + error.Message)
End Try

PostgreSQLDatabase.SSLAuthority

SSLAuthority As FolderItem

Specifies the root SSL certificate file.


PostgreSQLDatabase.SSLCertificate

SSLCertificate As FolderItem

Specifies the file for the client SSL certificate, replacing the default ~/.postgresql/postgresql.crt. This parameter is ignored if an SSL connection is not made.

This example specifies that the connection should try to use SSL using the specified certificate:

Var certFile As New FolderItem("LocalPGCertfile.crt")

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.SSLMode = PostgreSQLDatabase.SSLAllow
db.SSLCertificate = certFile
db.Port = 5432
db.DatabaseName = "BaseballLeague"
db.UserName = "broberts"
db.Password = "streborb"
Try
  db.Connect
  // Use the database
Catch error As DatabaseException
  MessageBox("Connection Failed: " + error.Message)
End Try

PostgreSQLDatabase.SSLKey

SSLKey As FolderItem

Specifies the location for the secret key used for the client certificate. It can specify a file that will be used instead of the default ~/.postgresql/postgresql.key file.

This example specifies that the connection should try to use SSL using the specified certificate and secret key:

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.SSLMode = PostgreSQLDatabase.SSLAllow
db.SSLCertificate = New FolderItem("LocalPGCertfile.crt")
db.SSLKey = New FolderItem("LocalKeyFile.key")
db.Port = 5432
db.DatabaseName = "BaseballLeague"
db.UserName = "broberts"
db.Password = "streborb"
Try
  db.Connect
  // Use the database
Catch error As DatabaseException
  MessageBox("Connection failed: " + error.Message)
End Try

PostgreSQLDatabase.SSLMode

SSLMode As Integer

Specifies the type of SSL connection to use to connect to the database server. Use the constant values SSLDisable, SSLAllow, SSLPrefer, SSLRequire, SSLVerifyCA and SSLVerifyFull.

These PostgreSQL constants are used to specify SSLMode:

  • SSLDisable (default): Only try a non-SSL connection

  • SSLAllow: First try a non-SSL connection; if that fails, try an SSL connection

  • SSLPrefer: First try an SSL connection; if that fails, try a non-SSL connection

  • SSLRequire: Only try an SSL connection. If a root CA file is present, verify the certificate in the same way as if verify-ca was specified

  • SSLVerifyCA: Only try an SSL connection, and verify that the server certificate is issued by a trusted CA

  • SSLVerifyFull: Only try an SSL connection, verify that the server certificate is issued by a trusted CA and that the server hostname matches that in the certificate

For more information about SSL usage with PostgreSQL, refer to Secure TCP/IP Connections with SSL.

This example specifies that the connection should try to use SSL:

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.SSLMode = PostgreSQLDatabase.SSLAllow
db.Port = 5432
db.DatabaseName = "BaseballLeague"
db.UserName = "broberts"
db.Password = "streborb"
Try
  db.Connect
  // Use the database
Catch error As DatabaseException
  MessageBox("Connection failed. Error: " + error.Message)
End Try

PostgreSQLDatabase.UserName

UserName As String

The username that is required for access to the database.

This code connects to a PostgreSQL database called “BaseballLeague”:

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.Port = 5432
db.DatabaseName = "BaseballLeague"
db.UserName = "broberts"
db.Password = "streborb"
Try
  db.Connect
  // Use the database
Catch error As DatabaseException
  // DB Connection error
  MessageBox(error.Message)
End Try

Method descriptions


PostgreSQLDatabase.AddRow

AddRow(tableName As String, row As DatabaseRow)

Inserts Data (a populated DatabaseRow) as a new row in TableName.

Always look for a DatabaseException to verify whether or not the data was added.

This code adds a row to an existing Team table with the columns “Name”, “Coach” and “City”. It’s assumed that the variable db contains an active database connection:

Var row As New DatabaseRow
// ID will be updated automatically
row.Column("Name") = "Penguins"
row.Column("Coach") = "Bob Roberts"
row.Column("City") = "Boston"

Try
  db.AddRow("Team", row)
Catch error As DatabaseException
  MessageBox("DB Error: " + error.Message)
End Try

PostgreSQLDatabase.BeginTransaction

BeginTransaction

Creates a new transaction. Changes to the database made after this call can be saved with CommitTransaction or undone with RollbackTransaction.

A DatabaseException will be raised if the transaction could not begin or another transaction is already in progress.

You typically want to Commit changes after ensuring there were no database errors:

// Prior DB code has run

Try
  DB.BeginTransaction
  DB.ExecuteSQL("CREATE TABLE AddressBook name VARCHAR, email VARCHAR")
  DB.CommitTransaction
Catch error As DatabaseException
  MessageBox("Error: " + error.Message)
  DB.RollbackTransaction
End Try

PostgreSQLDatabase.CheckForNotifications

CheckForNotifications

Checks to see if there are any waiting notifications. You will receive notifications as a ReceivedNotification event. If you would like to receive notifications automatically, create a Timer and use it to call CheckForNotifications repeatedly.

Notifications are received in the PostgreSQLDatabase event handler.

This example is in the Action event of a Timer.

// db is a previously connected PostgreSQLDatabase
db.CheckForNotifications

PostgreSQLDatabase.Close

Close

Closes or disconnects the database.

Calling Close does not issue a Commit, but some databases will automatically Commit changes in a transaction when you Close the connection and some database will automatically Rollback changes in a transaction when the connection is closed. Refer to the documentation for your database to check what its behavior is.

For desktop apps, you will often Connect to the database when the app starts and Close it when the app quits.

For web apps, you usually Connect to the database when the Session starts and Close it when the Session quits.

This code in the App.Closing event handler closes a previously connected database:

DB.Close // DB is a property on App

PostgreSQLDatabase.CommitTransaction

CommitTransaction

Commits an open transaction. This permanently saves changes to the database.

A DatabaseException will be raised if the transaction could not be committed.

You have to have an open transaction to be able to use CommitTransation. Use BeginTransaction to begin a transaction:

DB.BeginTransaction

You typically want to commit changes after ensuring there were no database errors:

// Prior DB code has run

Try
  DB.CommitTransaction
Catch error As DatabaseException
  MessageBox("Error: " + error.Message)
  DB.RollbackTransaction
End Try

PostgreSQLDatabase.Connect

Connect(Optional additionalOptions As String = "")

Connects to the database server and opens the database for access.

The additionalOptions are passed to PostgreSQL as is and are not escaped in any way. The available options for this parameter are specified here: http://www.postgresql.org/docs/9.1/static/libpq-connect.html

To connect using SSL, use the SSLMode, SSLCertificate and SSLKey properties as appropriate for how your server is configured.

This code attempts to connect to the “BaseballLeague” database on a PostgreSQL server:

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.Port = 5432
db.DatabaseName = "BaseballLeague"
db.UserName = "broberts"
db.Password = "streborb"
Try
  db.Connect
  // Use the database
Catch error As DatabaseException
  // There was a database connection error
  MessageBox(error.Message)
End Try

PostgreSQLDatabase.CreateLargeObject

CreateLargeObject As Integer

Creates a PostgreSQLLargeObject and returns its ID. Legal IDs are positive integers. Therefore, if CreateLargeObject returns 0 or less, then there was an error creating the large object. Use ErrorMessage to determine what the error was.

PostgreSQL requires that all large object operations be performed inside of a transaction. Therefore, you must start a transaction before you perform your first large object operation:

db.ExecuteSQL("BEGIN TRANSACTION")

After you have performed your last large object operation, you should close the transaction, like this:

db.ExecuteSQL("END TRANSACTION")

Please see the PostgreSQLLargeObject class for information on how to work with large objects.

Create a large object:

// db is an existing PostgreSQLDatabase connection
Try
  Var objectID As Integer = db.CreateLargeObject
Catch error As DatabaseException
  MessageBox("Couldn't create large object: " + db.ErrorMessage)
  Return
End Try

PostgreSQLDatabase.DeleteLargeObject

DeleteLargeObject(oid As Integer)

Deletes the large object identified by oid.

PostgreSQL requires that all large object operations be performed inside of a transaction. Therefore, you must start a transaction before you perform your first large object operation:

db.ExecuteSQL("BEGIN TRANSACTION")

After you have performed your last large object operation, you should close the transaction, like this:

db.ExecuteSQL("END TRANSACTION")

Please see the PostgreSQLLargeObject class for information on how to work with large objects.

Delete a large object:

// db is a previously connected PostgreSQLDatabase
// objectID is a reference to a previously created large object
Try
db.DeleteLargeObject(objectID)
Catch error As DatabaseException
  MessageBox("DB Error: " + error.Message)
End Try

PostgreSQLDatabase.ExecuteSQL

ExecuteSQL(ExecuteString As String)

Used to execute an SQL command. Use this for commands that do not return any data, such as CREATE TABLE or INSERT. SQLStatement contains the SQL statement.

In this example, the database is being updated without the use of parameters and thus leaves the database vulnerable to a SQL injection attack:

// Updates a table in a SQLite database (db)
Var sql As String
sql = "UPDATE Customer SET City='" + CityField.Text + "' WHERE PostalCode='" + PostalCodeField.Text + "'"
Try
 db.ExecuteSQL(sql)
Catch error As DatabaseException
  MessageBox("DB Error: " + error.Message)
End Try

Here’s the same example but using parameters which protects you against a SQL injection attack:

// Updates a table in a SQLite database (db)
Var sql As String
sql = "UPDATE Customer SET City=? WHERE PostalCode=?"
Try
 db.ExecuteSQL(sql, CityField.Text, PostalCode.Text)
Catch error As DatabaseException
  MessageBox("DB Error: " + error.Message)
End Try

The parameter values can also be passed in as a variant array:

Var sql As String
sql = "UPDATE Customer SET City=? WHERE PostalCode=?"
Var values(1) As Variant
values(0) = CityField.Text
values(1) = PostalCode.Text
Try
 db.ExecuteSQL(sql, values)
Catch error As DatabaseException
  MessageBox("DB Error: " + error.Message)
End Try

This code creates the Team table:

// db is a SQLite database
Var sql As String
sql = "CREATE TABLE Team (ID INTEGER NOT NULL, Name TEXT, Coach TEXT, City TEXT, PRIMARY KEY(ID));"
Try
  db.ExecuteSQL(sql)
  MessageBox("Team table created successfully.")
Catch error As DatabaseException
  MessageBox("DB Error: " + error.Message)
End Try

PostgreSQLDatabase.Listen

Listen(Name As String)

Listens for notifications named “Name”.

Used in conjunction with the Notify and CheckForNotifications methods. Notifications are received in the ReceivedNotification event handler.

Start listening for a specific notification:

// db is a previously connected PostgreSQLDatabase
db.Listen("RefreshAll")

PostgreSQLDatabase.Notify

Notify(Name As String)

Sends a notification named “Name”.

Clients that have used the Listen method for the name will be get the notification. To receive notifications, call the CheckForNotifications method. Notifications are received in the PostgreSQLDatabase event handler.

The Notify method does not have a way to send a payload. As a workaround you can call the command directly instead:

db.ExecuteSQL("NOTIFY ""UpdateCustomers"", '5, 10, 15'")

Send the “RefreshAll” notification:

// db is a previously connected PostgreSQLDatabase
db.Notify("RefreshAll")

PostgreSQLDatabase.OpenLargeObject

OpenLargeObject(oid As Integer, [ReadOnly As Boolean = False]) As PostgreSQLLargeObject

Opens the large object specified by the passed oid. The ReadOnly parameter is optional and defaults to False. If you pass True, then the large object will be opened in read-only mode. If OpenLargeObject is successful, it returns an instance of PostgreSQLLargeObject.

PostgreSQL requires that all large object operations be performed inside of a transaction. Therefore, you must start a transaction before you perform your first large object operation:

db.ExecuteSQL("BEGIN TRANSACTION")

After you have performed your last large object operation, you should close the transaction, like this:

db.ExecuteSQL("END TRANSACTION")

Please see the PostgreSQLLargeObject class for information on how to work with large objects.

Read the data in a large object:

// db is a previously connected PostgreSQLDatabase
// objectID is an integer referring to a previously created large object
Var largeObject As PostgreSQLLargeObject
largeObject = db.OpenLargeObject(objectID)

Var data As String = largeObject.Read(largeObject.Length)

PostgreSQLDatabase.Prepare

Prepare(statement As String) As PreparedSQLStatement

Creates a PreparedSQLStatement using the SQL statement for use with the various database prepared statement classes. A prepared statement is an SQL statement with parameters that has been pre-processed by the database so that it can be executed more quickly if it is re-used with different parameters. Prepared statements also mitigate the risk of SQL injection in web apps.

To create a prepared statement, you assign the value of Prepare to appropriate class for the database you are using.

Refer to the specific PreparedStatement class for the database you are using to learn how to specify and bind parameters:

If the provided SQL statement has an error, a DatabaseException will occur when you call SelectSQL or ExecuteSQL.

SQL Injection is a way to attack database-driven applications.

To create a prepared statement, you use the appropriate class for the database you are using. For example, to create a prepared statement for PostgreSQL:

// db is a previously connected PostgreSQLDatabase object

Var ps As PostgreSQLPreparedStatement
ps = db.Prepare("SELECT * FROM Country WHERE code = $1")

PostgreSQLDatabase.RollbackTransaction

RollbackTransaction

Cancels an open transaction restoring the database to the state it was in before the transaction began.

A DatabaseException will be raised if the rollback could not be completed.

You will generally want to rollback database changes if a DatabaseException occurs within the transaction.

You have to have an open transaction to be able to use Rollback. Call BeginTransaction to begin a transaction:

DB.BeginTransaction

This code uses rollback to revert database changes in a transaction when an error occurs:

// Prior DB code has run

Try
  db.CommitTransaction
Catch error As DatabaseException
  MessageBox("Error: " + error.Message)
  db.RollbackTransaction
End If

PostgreSQLDatabase.SelectSQL

SelectSQL(sql As String) As RowSet

SelectSQL(sql As String, [Paramarray values() As Variant]) As RowSet

Executes a SQL SELECT statement and returns the results in a RowSet. The sql parameter contains the SQL statement.

This sample adds the names of all customers in a particular postal code to a ListBox. It passes the entire SQL select as a single statement and appends the value from a TextField called PostalCode, leaving the database vulnerable to a SQL injection attack:

// db is a valid connection to a SQLite database
Var rowsFound As RowSet
Try
  rowsFound = db.SelectSQL("SELECT * FROM Customer WHERE PostalCode=" + PostalCode.Text)
  For Each row As DatabaseRow In rowsFound
    ListBox1.AddRow(row.Column("Name").StringValue)
  Next
  rowsFound.Close
Catch error As DatabaseException
  MessageBox("Error: " + error.Message)
End Try

This is the same code as above but instead it uses a value identifier ($1) and then passes the value in separately to avoid a SQL injection attack:

// db is a valid connection to a SQLite database
Var rowsFound As RowSet
Try
  rowsFound = db.SelectSQL("SELECT * FROM Customer WHERE PostalCode=$1", PostalCode.Text)
  For Each row As DatabaseRow In rowsFound
    ListBox1.AddRow(row.Column("Name").StringValue)
  Next
  rowsFound.Close
Catch error As DatabaseException
  MessageBox("Error: " + error.Message)
End Try

Multiple values can be passed to SelectSQL. In this example, Age and PostalCode are both DesktopTextField controls:

// db is a valid connection to a SQLite database
Var rowsFound As RowSet
Try
  rowsFound = db.SelectSQL("SELECT * FROM Customer WHERE Age=$1 AND PostalCode=$2", Age.Value, PostalCode.Text)
  For Each row As DatabaseRow In rowsFound
    ListBox1.AddRow(row.Column("Name").StringValue)
  Next
  rowsFound.Close
Catch error As DatabaseException
  MessageBox("Error: " + error.Message)
End Try

PostgreSQLDatabase.StopListening

StopListening(Name As String)

Stops listening for notifications named “Name”.

Tells the client to stop listening for the specified notification.

Stop listening for the “RefreshAll” notification:

// db is a previously connected PostgreSQLDatabase
db.StopListening("RefreshAll")

PostgreSQLDatabase.TableColumns

TableColumns(TableName As String) As RowSet

Returns a RowSet with information about all the columns (fields) in the specified TableName.

The following code creates a table and then display each column name one at a time:

Var db As SQLiteDatabase
Var dbFile As FolderItem

dbFile = New FolderItem("mydb.sqlite")
If dbFile.Exists Then
  dbFile.Remove
End If
db = New SQLiteDatabase
db.DatabaseFile = dbFile

Try
  db.CreateDatabase
  db.ExecuteSQL("CREATE TABLE Invoices (ID INTEGER, CustID INTEGER, Amount Double, Note TEXT)")

  Var columns As RowSet = db.TableColumns("Invoices")

  For Each c As DatabaseRow In columns
    MessageBox("Column: " + c.Column("ColumnName").StringValue)
  Next
Catch error As IOException
  MessageBox("The database could not be created: " + error.Message)
Catch error As DatabaseException
  MessageBox("Database error: " + error.Message)
End Try

PostgreSQLDatabase.TableIndexes

TableIndexes(TableName As String) As RowSet

Returns a RowSet containing the list of indexes for the passed TableName. Returns Nil if the table has no indexes or the database source does not support indexes.

A DatabaseException will be raised if an error occurs.

The RowSet returns one row for each index on the table and it has one field: IndexName As String.

This code displays the indexes for the “Invoices” table (if it exists) in the specified SQLite database:

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

If dbFile <> Nil Then
  Var db As New SQLiteDatabase
  Try
    db.Connect
    Var indexRS As RowSet
    indexRS = db.TableIndexes("Invoices") // A table with indexes in the SQLite DB
    For Each row As DatabaseRow In indexRS
      MessageBox("Index: " + row.ColumnAt(0).StringValue)
    Next
    indexRS.Close
  Catch error As NilObjectException
    MessageBox("This database has no indexes.")
  Catch error As DatabaseException
    MessageBox("Could not connect to database. Error: " + error.Message)
  End Try
End If

PostgreSQLDatabase.Tables

Tables As RowSet

Returns a RowSet with a list of all tables in the database.

A DatabaseException will be raised if an error occurs.

Tables returns a RowSet with one field: TableName As String.

The following code gets and displays the table names for the connected database:

// App.db is a connected database
Var tables As RowSet
tables = App.db.Tables

Try
  For Each row As DatabaseRow In tables
    MessageBox(row.ColumnAt(0).StringValue)
  Next
  tables.Close
Catch error As NilObjectException
  MessageBox("This database has no tables.")
End Try

Event descriptions


PostgreSQLDatabase.ReceivedNotification

ReceivedNotification(Name As String, ID As Integer, Extra As String)

A notification named aName has been received. The process ID of the sender is passed in the aPid parameter.

Used by the Listen and Notify PostgreSQL feature.

To receive notifications, use the Listen method to specify the name of each notification to listen for and call the CheckForNotifications method (usually in a Timer).

Notifications are sent using the Notify method.

The aExtra parameter may contain payload information provided by another PostgreSQL system. Xojo does not have a way to provide a payload to a notification. as a workaround you can manually call NOTIFY yourself like this:

db.ExecuteSQL("NOTIFY ""UpdateCustomers"", '5, 10, 15'")

This code in the ReceivedNotification event handler displays the notification that was received:

MessageBox("Received notification: " + sName + " from " + aPid.ToString)

Notes

In order to use this class, you must have the PostgreSQLPlugin database plug-in in your plugins folder.

The PostgreSQL plug-in supports MD5 password authentication.

A field specified as type Float is actually implemented by PostgreSQL as a double (8 bytes).

PostgreSQL dates: field types Date, Time, and TimeStamp are supported by using the DatabaseColumn method for a date representation of this data, or use StringValue to get a human-readable date and/or time.

The PostgreSQLDatabase engine supports only the MoveNext RowSet navigation method.

Also be sure to refer to the official PostgreSQL documentation.


Threading

SelectSQL and ExecuteSQL statements do not block when called from within Threads.


Xojo cloud

Xojo Cloud includes built-in support for PostgreSQL databases, which you can enable in your The Xojo Cloud Control Panel.

To access an external PostgreSQL databases from web apps running on Xojo Cloud, you will first have to use the FirewallPort class to open the port used to connect to PostgreSQL, which is usually 5432.

Var fwp As New XojoCloud.FirewallPort(5432, _
  XojoCloud.FirewallPort.Direction.Outgoing)
fwp.Open // This call is synchronous
If fwp.IsOpen Then
  // Do what you need to do
End If

Refer to the The Xojo Cloud Control Panel topic for more information about using PostgreSQL with Xojo Cloud.


Large objects

PostgreSQLDatabase implements PostgreSQL’s large objects. PostgreSQL requires that all large object operations be performed inside of a transaction. Therefore, you must start a transaction before you perform your first large object operation:

db.ExecuteSQL("BEGIN TRANSACTION")

After you have performed your last large object operation, you should close the transaction, like this:

db.ExecuteSQL("END TRANSACTION")

Please see the PostgreSQLLargeObject class for information on how to work with large objects.


Listen and notify protocol

The PostgreSQLDatabase class implements the listen and notify protocol of PostgreSQL databases.

To send a notification, call the PostgreSQLDatabase.Notify method with the name of the notification you want to send. For example, if you wanted to send a notification called “Hello World”, you would call Notify like this:

// db is a previously connected PostgreSQL database
db.Notify("Hello World")

To check for notifications, call the PostgreSQLDatabase.CheckForNotifications method. You will receive notifications as a PostgreSQLDatabase.ReceivedNotification event. The ReceivedNotification event is called with three arguments: the name of the notification as a String, the ID of the process sending the notification as an Integer, and an extra argument that the PostgreSQL documentation says is not used at this time. The name parameter is the same name that you used with the Notify method.

If you would like to check for notifications automatically, at some set interval, then use a Timer and call CheckForNotifications in its Action event.

Sample code

This code connects to an existing PostgreSQL database.

Var db As New PostgreSQLDatabase
db.Host = "192.168.1.172"
db.Port = 5432
db.DatabaseName = "myDatabase"
db.AppName = "MyApp"
db.Username = "Charlie"
db.Password = "mashie"
Try
 db.Connect Then
  //proceed with database operations
Catch error As DatabaseException
  MessageBox("The connection failed.")
End Try

Compatibility

All projects types on all supported operating systems.