2
I’ve been realizing that I’m having trouble recording information in the bank because:
-I’m running a block of a method that records data in the database:
//codigo aqui
MySQLCallDB.InsertData("PEDIDOS", columns, values); //gravando informações
//codigo quebra por alguma Exception aqui ou seja, terei um Pedido sem Detalhes
MySQLCallDB.InsertData("PEDIDOS_DETALHES", columns, values); //gravando informações
//Restante do código
The method InsertData
is a static class that manages queries in my application(MySQLCallDB
) and the code and the way I call MYSQL in InsertData
is:
public static string InsertData(string table, List < string > columns, List < string > values) {
string Query = "QUERY";
Query += "SELECT LAST_INSERT_ID() as ID;";
MySqlCommand comm = new MySqlCommand("", conexao);
comm.CommandText = Query;
try {
conexao.Open();
MySqlDataReader reader = comm.ExecuteReader();
reader.Read();
return reader.GetString("ID");
}
catch (Exception e) {
Debug.WriteLine(Query);
Debug.WriteLine(e);
return "Error";
}
finally {
conexao.Close(); //Fechando a conexão}}
}
}
The question is: in cases where an exception occurs that breaks the whole process (as described in the code comments), what should I do to not have information without logic within the database?
Use Begintransaction - Commit and catch Rollback inside the Ry. See this example: https://dev.mysql.com/doc/dev/connector-net/8.0/html/M_MySql_Data_MySqlClient_MySqlConnection_BeginTransaction.htm
– Netinho Santos
I believe the answer to that question can help you.
– Thiago Magalhães
That’s it. You have to do the Inserts and updates within a transaction according to the ACID model. https://www.diegomacedo.com.br/tag/atomicity/
– Reginaldo Rigo