dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlConnection Class / Ping Method
Example

Ping Method
Determines whether connection to PostgreSQL server is valid.
Syntax
'Declaration
 
Public Function Ping() As Boolean
 

Return Value

true, if the connection is valid; otherwise, false.
Remarks

If the application has an open connection for a long time, it is recommended to check a connection for validation before using it. Call of Devart.Data.PostgreSqll.PgSqlConnection.Ping method has single round trip on PostgreSQL server.

It is not a good idea to keep a connection open for long time. Connection pool automatically pings all connections within, thus closing old connections and opening new ones is encouraged.

Example
This code sample shows how to check for server availability before issuing a query.
public void ExecuteSafely(MySqlConnection myConnection)
{
  MySqlCommand myCommand = new MySqlCommand("INSERT INTO Test.Dept(DeptNo, DName) Values('50', 'DEVELOPMENT')");
  myCommand.Connection = myConnection;
  try
  {
    if (myConnection.Ping())
    {
      myCommand.ExecuteNonQuery();
      Console.WriteLine("Successfully executed");
    }
    else
    {
      myConnection.Close();
      Console.WriteLine("Unable to ping the server");
    }
  }
  catch
  {
    myConnection.Close();
    Console.WriteLine("Error during query execution");
  }
}
Public Sub ExecuteSafely(ByVal myConnection As MySqlConnection)
  Dim myCommand As New MySqlCommand("INSERT INTO Test.Dept(DeptNo, DName) Values('50', 'DEVELOPMENT')")
  myCommand.Connection = myConnection
  Try
    If (myConnection.Ping()) Then
      myCommand.ExecuteNonQuery()
      Console.WriteLine("Successfully executed")
    Else
      myConnection.Close()
      Console.WriteLine("Unable to ping the server")
    End If
  Catch
    myConnection.Close()
    Console.WriteLine("Error during query")
  End Try
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also