How to make database transaction in Webforms?

Asked

Viewed 51 times

1

I got a stock in a file .aspx that runs multiple Inserts in the bank. I need that if something fails, the operation is canceled, since one record will depend on the other.

I usually use a transaction function to do this in other languages/frameworks (example, PHP Laravel).

It is possible to do a transaction in C#/Webforms?

  • Yes, but I don’t quite understand how you perform your Inserts , they are called one by one per event ?

  • @Marconciliosouza is like this: I create an Insert for the table "ordem_servicos", then I have to create several records related to this "ordem_servico". I need to make sure that everything went well. Should any exception happen, I want to give a "rollback"

  • I know it’s transaction that solves this, but I don’t know how to do C#, you know? I’m starting now :p

  • in general you need to have your Insert in a Transactionscope see the question you had asked... https://answall.com/q/162465/43340

1 answer

1

The example below is using Dapper.

using (var transactionScope = new TransactionScope())
{
    using (var conexao = new SqlConnection(Configuracao.Configuracoes.StringDeConexaoSQLServer))
    {
        conexao.Open();

        sql = @"INSERT INTO  ..."; // 11
        conexao.Execute(sql, new { });
        conexao.Close();
    }

    transactionScope.Complete();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.