Sobes.tech
Junior — Middle

Have you used transaction management mechanisms in SQL databases in your projects?

sobes.tech AI

Answer from AI

Yes, I have used transaction management mechanisms in SQL databases in my projects to ensure data integrity and correct execution of operations. For example, with Entity Framework in C#, I used transactions to group multiple insert or update operations so that either all changes are applied or, in case of an error, rolled back.

Example of using a transaction in C# with ADO.NET:

using (var connection = new SqlConnection(connectionString))
{
    connection.Open();
    using (var transaction = connection.BeginTransaction())
    {
        try
        {
            // Execute multiple commands within the transaction
            var command1 = new SqlCommand("INSERT INTO Table1 ...", connection, transaction);
            command1.ExecuteNonQuery();

            var command2 = new SqlCommand("UPDATE Table2 ...", connection, transaction);
            command2.ExecuteNonQuery();

            transaction.Commit();
        }
        catch
        {
            transaction.Rollback();
            throw;
        }
    }
}

This guarantees that changes are atomic and the database remains in a consistent state.