Return Value
true, if the connection is valid; otherwise, false.
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.
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