dotConnect for PostgreSQL Documentation
PgSqlConnection Class
Members  Example  See Also  support@devart.com
Devart.Data.PostgreSql Namespace : PgSqlConnection Class

Represents an open connection to PostgreSQL.

Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.Common.DbConnection
            Devart.Common.DbConnectionBase
               Devart.Data.PostgreSql.PgSqlConnection

Syntax

Visual Basic (Declaration) 
Public Class PgSqlConnection 
   Inherits Devart.Common.DbConnectionBase
   Implements IComponentIDbConnectionICloneableIDisposable 

Remarks

A PgSqlConnection object represents a unique connection to PostgreSQL. Use it in conjunction with PgSqlCommand, PgSqlDataReader, PgSqlDataAdapter or other components for convenient interoperation with PostgreSQL.

When you create an instance of PgSqlConnection, all properties are set to their initial values. For a list of these values, see ConnectionString.

If the PgSqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close.

Note that PgSqlConnection instance is not guaranteed to be thread safe. You should avoid using the same PgSqlConnection in several threads at the same time. It is recommended to open a new connection per thread and to close it when the work is done. Actually, connections will not be created/disposed every time with the Pooling=true; connection string option - connections will be stored at connection pool. This boosts performance greatly.

A single PgSqlConnection can be used by many PgSqlCommand objects on the assumption that all operations will be done consecutively. In other words, you can not execute a SQL statement while an asynchronous operation is in progress.

This class supports cross-form data binding with the InterForm Technology.

Example

The following example creates a PgSqlCommand and a PgSqlConnection. The PgSqlConnection is opened and set as the Connection property. The example then calls Devart.Common.DbCommandBase.ExecuteNonQuery method, and closes the connection. To accomplish this, the Devart.Common.DbCommandBase.ExecuteNonQuery is passed a connection string and a query string that is SQL INSERT statement.
C#csharpCopy Code
public void InsertRow(string pgConnectionString)
{
  // If the connection string is empty, use default.
  if(pgConnectionString == "")
  {
    pgConnectionString = 
        "host=server;database=test;user id=postgres;";
  }
  PgSqlConnection myConn = new PgSqlConnection(pgConnectionString);
  string myInsertQuery = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')";
  PgSqlCommand pgCommand = new PgSqlCommand(myInsertQuery);
  pgCommand.Connection = myConn;
  myConn.Open();
  try
  {
    pgCommand.ExecuteNonQuery();
  }
  finally
  {
    myConn.Close();
  }
}
C#Visual BasicCopy Code
Public Sub InsertRow(pgConnectionString As String)
  ' If the connection string is empty, use default.
  If pgConnectionString = "" Then
    pgConnectionString = _
        "host=server;database=test;user id=postgres;"
  End If
  Dim myConn As New PgSqlConnection(pgConnectionString)
  Dim myInsertQuery As String = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')"
  Dim pgCommand As New PgSqlCommand(myInsertQuery)
  pgCommand.Connection = myConn
  myConn.Open()
  Try
    pgCommand.ExecuteNonQuery()
  Finally
    myConn.Close()
  End Try
End Sub

Requirements

Namespace: Devart.Data.PostgreSql

Platforms:Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7, Windows 8, Windows 10, Windows Server 2012 family.

See Also