public Microsoft.Data.SqlClient.SqlConnection CerrarConexion() { if (conex.State == ConnectionState.Open) { conex.Close(); } return(conex); }
private void OpenWithCheck(string checkQueryString) { bool connectionChecked = false; bool restoreTriggered = false; while (!connectionChecked) { base.Open(); try { using (var command = underlyingConnection.CreateCommand()) { command.CommandText = checkQueryString; command.ExecuteNonQuery(); } connectionChecked = true; } catch (Exception exception) { if (SqlHelper.ShouldRetryOn(exception)) { if (restoreTriggered) { SqlLog.Error(exception, Strings.LogConnectionRestoreFailed); throw; } SqlLog.Warning(exception, Strings.LogGivenConnectionIsCorruptedTryingToRestoreTheConnection); var newConnection = new SqlServerConnection(underlyingConnection.ConnectionString); try { underlyingConnection.Close(); underlyingConnection.Dispose(); } catch { } underlyingConnection = newConnection; restoreTriggered = true; continue; } else { throw; } } } }
private static bool TryReconnect(ref SqlServerConnection connection, string query) { try { var newConnection = new SqlServerConnection(connection.ConnectionString); try { connection.Close(); connection.Dispose(); } catch { } connection = newConnection; connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = query; command.ExecuteNonQuery(); } return(true); } catch (Exception) { return(false); } }