private string UpdateValorAnuidade(int id, ValorAnuidade v) { bool _resultado = false; string _msg = ""; using (SqlConnection connection = new SqlConnection(strConnSql)) { connection.Open(); SqlCommand command = connection.CreateCommand(); SqlTransaction transaction; // Start a local transaction. transaction = connection.BeginTransaction("AtualizarValorAnuidade"); command.Connection = connection; command.Transaction = transaction; try { command.CommandText = "" + "UPDATE dbo.AD_Valor_Anuidade " + "SET Valor = @Valor " + "WHERE ValorAnuidadeId = @id"; command.Parameters.AddWithValue("Valor", v.Valor); command.Parameters.AddWithValue("id", id); int x = command.ExecuteNonQuery(); _resultado = x > 0; _msg = x > 0 ? "Atualização realizada com sucesso" : "Atualização NÃO realizada com sucesso"; transaction.Commit(); // Log do UPDATE VALOR_ANUIDADE: /* Log comentado para melhorar a performance do cadastro de anuidade * StringBuilder sb = new StringBuilder(); * sb.Append("Parâmetros: "); * * for (int z = 0; z < command.Parameters.Count; z++) * { * sb.Append(command.Parameters[z].ParameterName + ": " + command.Parameters[z].Value + ", "); * } * * _instrucaoSql = sb.ToString(); * _result = x > 0 ? "SUCESSO" : "FALHA"; * * string log = logRep.SetLogger(className + "/UpdateValorAnuidade", * "UPDATE", "VALOR_ANUIDADE", id, _instrucaoSql, _result);*/ //Fim do Log } catch (Exception ex) { try { transaction.Rollback(); } catch (Exception ex2) { throw new Exception($"Rollback Exception Type:{ex2.GetType()}. Erro:{ex2.Message}"); } string log = logRep.SetLogger(className + "/UpdateValorAnuidade", "UPDATE", "VALOR_ANUIDADE", 0, ex.Message, "FALHA"); throw new Exception($"Commit Exception Type:{ex.GetType()}. Erro:{ex.Message}"); } finally { connection.Close(); } } return(_msg); }
private string InsertValorAnuidade(ValorAnuidade v) { bool _resultado = false; string _msg = ""; Int32 id = 0; string _ident = ""; using (SqlConnection connection = new SqlConnection(strConnSql)) { connection.Open(); SqlCommand command = connection.CreateCommand(); SqlTransaction transaction; // Start a local transaction. transaction = connection.BeginTransaction("IncluirValorAnuidade"); command.Connection = connection; command.Transaction = transaction; try { command.CommandText = "" + "INSERT into dbo.AD_Valor_Anuidade (Valor, TipoAnuidade, AnuidadeTipoPublicoId) " + "VALUES (@Valor, @TipoAnuidade, @AnuidadeTipoPublicoId) " + "SELECT CAST(scope_identity() AS int) "; command.Parameters.AddWithValue("Valor", v.Valor); command.Parameters.AddWithValue("TipoAnuidade", v.TipoAnuidade); command.Parameters.AddWithValue("AnuidadeTipoPublicoId", v.AnuidadeTipoPublicoId); id = (Int32)command.ExecuteScalar(); _resultado = id > 0; if (id > 0) { _ident = _ident.PadLeft(10 - id.ToString().Length, '0') + id.ToString(); } _msg = id > 0 ? $"{_ident}Inclusão realizada com sucesso" : $"{_ident}Inclusão Não realizada com sucesso"; transaction.Commit(); // Log da Inserção VALOR_ANUIDADE: /* Log comentado para aumentar a performance do cadastro de anuidade: * StringBuilder sb = new StringBuilder(); * sb.Append("Parâmetros: "); * * for (int z = 0; z < command.Parameters.Count; z++) * { * sb.Append(command.Parameters[z].ParameterName + ": " + command.Parameters[z].Value + ", "); * } * * _instrucaoSql = sb.ToString(); * _result = id > 0 ? "SUCESSO" : "FALHA"; * * string log = logRep.SetLogger(className + "/InsertValorAnuidade", * "INSERT", "VALOR_ANUIDADE", id, _instrucaoSql, _result);*/ //Fim do Log } catch (Exception ex) { // Attempt to roll back the transaction. try { transaction.Rollback(); } catch (Exception ex2) { throw new Exception($"Rollback Exception Type:{ex2.GetType()}. Erro:{ex2.Message}"); } string log = logRep.SetLogger(className + "/InsertValorAnuidade", "INSERT", "VALOR_ANUIDADE", 0, ex.Message, "FALHA"); throw new Exception($"Commit Exception Type:{ex.GetType()}. Erro:{ex.Message}"); } finally { connection.Close(); } } return(_msg); }