A more robust solution and complete what I suggest. Knowing that everything has a quick, easy and wrong solution, better waste time on implementation for quality gain!
#region Controle de Transação
using (OracleConnection connection = new OracleConnection(ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString))
using (OracleCommand command = connection.CreateCommand())
{
try
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
#region excluindo registro da tabela 1'
command.CommandText = Constantes.sql1; //Constantes é uma classe estatica com declarações de variaveis ou constantes estaticas onde centraliza os scripts.
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("p_codigo ", OracleDbType.int, ParameterDirection.Input)).Value = codigo;
command.ExecuteNonQuery();
command.Parameters.Clear();
#endregion
#region excluindo registro da tabela 2'
command.CommandText = Constantes.sql2;
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("p_Numreq", OracleDbType.int, ParameterDirection.Input)).Value = codigo;
command.ExecuteNonQuery();
command.Parameters.Clear();
#endregion
#region excluindo registro da tabela n'
command.CommandText = Constantes.sql3;
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("p_Numreq", OracleDbType.int, ParameterDirection.Input)).Value = codigo;
command.ExecuteNonQuery();
command.Parameters.Clear();
#endregion
transaction.Commit();
}
#endregion
}
//Nestes casos não devolve o requisitorio então não deve atualizar a situação do mesmo!
catch (OracleException oe)
{
var retornoAmigavel = Constantes.Error;
switch (oe.ErrorCode)
{
case 12170:
retornoAmigavel = oe.ErrorCode +"-"+ Constantes.SemConexao;
break;
case 28000:
retornoAmigavel = oe.ErrorCode + "-" + Constantes.ContaBloqueada;
break;
case 1073:
retornoAmigavel = oe.ErrorCode + "-" + Constantes.ErroDeConexao;
break;
case 1:
retornoAmigavel = oe.ErrorCode + "-" + Constantes.ChavePrimariaViolada;
break;
default:
retornoAmigavel = Constantes.ErroGenerico + oe.Message;
break;
}
Console.WriteLine(retornoAmigavel, oe);
}
catch (Exception e)
{
var EretornoAmigavel = "ERROR REQ.Nº: " + requ.numero;
EretornoAmigavel += "\nConstantes.ErroGenerico " + e.Message;
Console.WriteLine(EretornoAmigavel);
}
}
#endregion
That’s it! I hope I contributed...
Debug to know exactly the error, is the code coming correctly? Here I ran a command with more than one delete and it worked normal.
– Maicon Carraro
The default of your project is to build SQL queries in C# and only run in the bank or you can have a database process that runs only in c#?
– Intruso