public static int ExecuteScalarIntWithRetry(this SqlCommand Cmd) { int ret = 0; try { Cmd.Connection.OpenWithRetry(); int MaxRetry = 3; for (int i = 0; i < MaxRetry; i++) { try { ret = System.Convert.ToInt16(Cmd.ExecuteScalar()); //Logging.LogInformation("SqlExecution Succeeded.. Exiting Retry Loop"); i = MaxRetry; } catch (SqlException e) { if (TransientDapperExtensions.DetermineIfRetry(e, ref i, MaxRetry) == false) { i = MaxRetry; } ; } } } catch (SqlException e) { if (e.Number == -2) { //Logging.LogErrors(new Exception(String.Format("Timeout during execution of SQL:{0}. Timeout was set to {1}", Cmd.CommandText.ToString(), Cmd.CommandTimeout.ToString()))); throw new Exception("Timeout during execution of SQL:" + Cmd.CommandText.ToString() + ". Timeout was set to " + Cmd.CommandTimeout.ToString()); } else { //Logging.LogErrors(new Exception(String.Format("Timeout during execution of SQL:{0}. Error was {1}", Cmd.CommandText.ToString(), e.Message.ToString()))); throw new Exception("Error during execution of SQL:" + Cmd.CommandText.ToString() + ". Error was " + e.Message.ToString()); } } return(ret); }
public static int ExecuteNonQueryWithVerboseThrow(this SqlCommand Cmd) { int ret = 0; try { Cmd.Connection.OpenWithRetry(); int MaxRetry = 3; for (int i = 0; i < MaxRetry; i++) { try { Cmd.ExecuteNonQuery(); //Utilities.StaticMethods.LogProvider.GetLog().Info("SqlExecution Succeeded.. Exiting Retry Loop"); i = MaxRetry; } catch (SqlException e) { if (TransientDapperExtensions.DetermineIfRetry(e, ref i, MaxRetry) == false) { i = MaxRetry; } ; } } } catch (SqlException e) { if (e.Number == -2) { //Logging.LogErrors(new Exception(String.Format("Timeout during execution of SQL:{0}. Timeout was set to {1}", Cmd.CommandText.ToString(), Cmd.CommandTimeout.ToString()))); throw new Exception("Timeout during execution of SQL:" + Cmd.CommandText.ToString() + ". Timeout was set to " + Cmd.CommandTimeout.ToString()); } else { //Logging.LogErrors(new Exception(String.Format("Timeout during execution of SQL:{0}. Error was {1}", Cmd.CommandText.ToString(), e.Message.ToString()))); throw new Exception("Error during execution of SQL:" + Cmd.CommandText.ToString() + ". Error was " + e.Message.ToString()); } } return(ret); }