public object ExecuteProcedure(string procedureName, ExecuteType executeType, IDictionary <string, object> parameters) { using (SqlConnection _conn = new SqlFactoryConnection().Connection()) { try { object returnObject = null; SqlCommand Command = new SqlCommand(procedureName, _conn) { CommandType = CommandType.StoredProcedure, }; if (parameters != null) { Command.Parameters.Clear(); foreach (KeyValuePair <string, object> kvp in parameters) { Command.Parameters.Add(new SqlParameter(kvp.Key, kvp.Value)); } } switch (executeType) { case ExecuteType.ExecuteNonQuery: returnObject = Command.ExecuteNonQuery(); break; case ExecuteType.ExecuteScalar: returnObject = Command.ExecuteScalar(); break; } return(returnObject); } catch (Exception ex) { Logger?.LogError("Error de ejecución en modo sincrono ExecuteProcedure"); throw new ArgumentException(ex.Message, ex); } finally { if (_conn.State == ConnectionState.Open) { Logger?.LogInformation("Cerrando conexión en modo sincrono ExecuteProcedure"); _conn.Close(); } } } }
public int ExecuteNonQuery(string procedureName, Dictionary <string, object> parameters) { int returnObject = 0; using (SqlConnection _conn = new SqlFactoryConnection().Connection()) { using (SqlCommand Command = new SqlCommand(procedureName, _conn)) { try { Command.CommandType = CommandType.StoredProcedure; if (parameters != null) { Command.Parameters.Clear(); foreach (KeyValuePair <string, object> kvp in parameters) { Command.Parameters.Add(new SqlParameter(kvp.Key, kvp.Value)); } } returnObject = Command.ExecuteNonQuery(); } catch (Exception ex) { Logger?.LogError($"Error en ejecución del siguiente Procedimiento {procedureName} se produjo el siguiente error: {ex.Message}"); throw new ArgumentException(ex.Message, ex); } finally { if (_conn.State == ConnectionState.Open) { Logger?.LogInformation("Cerrando conexión en modo sincrono ExecuteNonQuery"); _conn.Close(); } } } return(returnObject); } }