dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlConnection Class / Ping Method
Example

In This Topic
    Ping Method
    In This Topic
    Determines whether connection to MySQL server is valid.
    Syntax
    'Declaration
     
    Public Function Ping() As Boolean
    public bool Ping()

    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 Ping method has single round trip on MySQL 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.

    This method does not throw exceptions. It does not close the connection if it is invalid, it just marks it to forbid putting the invalid connection to the pool when closing.

    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