This event occurs when connection is lost.
The event handler receives an argument of type ConnectionLostEventArgs containing data related to this event. The following ConnectionLostEventArgs properties provide information specific to this event.
Property | Description |
---|
AttemptNumber | The number of attempts to reconnect. |
Cause | The reason of the connection loss. |
Component | The object where the connection loss occurs. |
Context | The state of connection when connection is lost. |
RetryMode | The application behavior when connection is lost. |
MySqlConnection conn = new MySqlConnection("user id=root;password=root;port=3306;host=localhost;database=test");
conn.LocalFailover = true;
conn.ConnectionLost += new ConnectionLostEventHandler(conn_ConnectionLost);
static void conn_ConnectionLost(object sender, ConnectionLostEventArgs e) {
if (e.Cause == ConnectionLostCause.Execute) {
if (e.Context == ConnectionLostContext.None)
e.RetryMode = RetryMode.Reexecute;
else
e.RetryMode = RetryMode.Raise;
}
else
e.RetryMode = RetryMode.Raise;
}
Dim conn As MySqlConnection = New MySqlConnection("user id=root;password=root;port=3306;host=localhost;database=test")
conn.LocalFailover = True
AddHandler conn.ConnectionLost, AddressOf conn_ConnectionLost
Sub conn_ConnectionLost(ByVal sender As Object, ByVal e As ConnectionLostEventArgs)
If e.Cause = ConnectionLostCause.Execute Then
If e.Context = ConnectionLostContext.None Then
e.RetryMode = RetryMode.Reexecute
Else
e.RetryMode = RetryMode.Raise
End If
Else
e.RetryMode = RetryMode.Raise
End If
End Sub