---
title: "SQL Transaction Commands in ADO.NET"  
description: "SQL transactions are used in ADO.NET - this is a powerful feature that ensures data integrity during various database operations."  
author: "Ashutosh Patel"  
published: 2025-04-23  
updated: 2025-04-23  
canonical: https://www.mindstick.com/blog/305512/sql-transaction-commands-in-ado-dot-net  
category: "ado.net"  
tags: ["ado.net", "Transactions"]  
reading_time: 2 minutes  

---

# SQL Transaction Commands in ADO.NET

[**SQL transactions**](https://www.mindstick.com/blog/304493/explain-the-transaction-in-sql-server) are used in `ADO.NET` - this is a powerful feature that ensures [data integrity](https://www.mindstick.com/forum/160197/what-is-the-normalization-in-sql-server-explain-its-impact-on-data-integrity-and-performance) during various [database operations](https://www.mindstick.com/forum/160213/explain-the-use-of-asynchronous-database-operations-in-dot-net-core-apis).

A **[transaction](https://www.mindstick.com/blog/175/transactions-in-database)** is a sequence of database operations that must be executed as a unit. It follows the **ACID** [principles](https://www.mindstick.com/interview/34041/understanding-solid-principles-in-object-oriented-design):

- **A**tomicity: all operations succeed or none
- **C**onsistency: The database remains consistent
- **I**solation: Transactions are [independent](https://answers.mindstick.com/qa/98599/what-is-the-longest-river-in-the-commonwealth-of-independent-states)
- **D**urability: changes persist even after a crash

**SQL Transaction Commands in ADO.NE**T

ADO.NET uses the following commands to manage transactions,

| **Command** | **Purpose** |
| --- | --- |
| `BeginTransaction()` | Starts a new transaction |
| `Commit()` | Saves all changes made in the transaction |
| `Rollback()` | Reverts all changes if any error occurs |

## Basic Example of Using Transaction in ADO.NET

Let's say you want to **transfer money** between two bank accounts,

```cs
using System;
using System.Data;
using System.Data.SqlClient;
class TransactionExample
{
   static void Main()
   {
       string connectionString = "your_connection_string_here";
       using (SqlConnection conn = new SqlConnection(connectionString))
       {
           conn.Open();
           // Start a local transaction
           SqlTransaction transaction = conn.BeginTransaction();
           try
           {
               // Command 1: Deduct from Account A
               SqlCommand cmd1 = new SqlCommand("UPDATE Accounts SET Balance = Balance - 500 WHERE AccountID = 1", conn, transaction);
               cmd1.ExecuteNonQuery();
               // Command 2: Add to Account B
               SqlCommand cmd2 = new SqlCommand("UPDATE Accounts SET Balance = Balance + 500 WHERE AccountID = 2", conn, transaction);
               cmd2.ExecuteNonQuery();
               // If both succeed, commit
               transaction.Commit();
               Console.WriteLine("Transaction committed successfully.");
           }
           catch (Exception ex)
           {
               // If any command fails, roll back the transaction
               transaction.Rollback();
               Console.WriteLine("Transaction rolled back. Error: " + ex.Message);
           }
       }
   }
}
```

**[Explanation](https://yourviews.mindstick.com/view/84608/what-are-hindenburg-s-allegations-against-adani-detailed-explanation):**

- `conn.BeginTransaction()`: Begins a SQL transaction.
- `new SqlCommand(..., conn, transaction)`: Associates command with the transaction.
- `transaction.Commit()`: Saves changes to the Database.
- `transaction.Rollback()`: Undoes the changes if any occurred.

## Important Points While using Transaction

- Always use `try-catch` during transactions
- Always `commit` or `rollback` to avoid open transactions
- Keep transactions small to avoid locking [resources](https://yourviews.mindstick.com/view/293/renovation-causes-wastage-of-resources)
- Use `using` statements to ensure the [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) is closed.

Also, read: [ACID Properties in Database Transactions](https://www.mindstick.com/articles/338513/acid-properties-in-database-transactions)

---

Original Source: https://www.mindstick.com/blog/305512/sql-transaction-commands-in-ado-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
