Sobes.tech
Junior — Middle

Do you have experience using transactions in database management systems?

sobes.tech AI

Answer from AI

Yes, in C#, classes like TransactionScope or transactions provided by specific database providers, such as SqlTransaction for SQL Server, are often used for working with transactions in databases.

A transaction allows grouping multiple database operations into a single unit that either fully completes or fully rolls back in case of an error, ensuring data integrity.

Example using TransactionScope:

using (var scope = new TransactionScope())
{
    using (var connection = new SqlConnection(connectionString))
    {
        connection.Open();

        var command1 = new SqlCommand("INSERT INTO Table1 ...", connection);
        command1.ExecuteNonQuery();

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

    scope.Complete(); // Confirm the transaction
}

If an exception occurs within the block and scope.Complete() is not called, all changes will be rolled back.

Do you have experience using transactions in database… - sobes.tech