How to do database transaction in WebForms?

1

I have an action on a .aspx file that runs multiple inserts in the database. I need that, in case 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 (eg, PHP's Laravel).

Is it possible to do a transaction in C # / Webforms?

    
asked by anonymous 02.05.2018 / 21:37

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();
}
    
03.05.2018 / 13:14