Transactions are integral to database management, ensuring data integrity and consistency. SQL, in combination with .NET languages like C#, offers powerful transaction management capabilities. In C#, we leverage `SqlConnection`, `SqlCommand`, and `SqlTransaction` classes to implement transactions. Let's dive into an example that demonstrates how to work with SQL transactions in C#.

First, ensure you have added the `System.Data.SqlClient` namespace to your C# code file to work with SQL-related classes. Here's a simple example of starting a transaction and rolling it back:

Basic SQL Transaction in C#
transactions are started when a new `SqlCommand` is constructed with a `SqlTransaction` object, which is typically obtained from a `SqlConnection` object. This initializes the transaction context.

Here's a C# example that demonstrates starting a transaction and rolling it back:
Starting a Transaction

First, let's establish a connection to the SQL database:
```csharp using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); // Create a transaction through the connection using (SqlTransaction transaction = conn.BeginTransaction()) { // Rest of the transactional operations go here } } ```
As seen above, `BeginTransaction()` is called on the connection object to start a new transaction. The transaction is automatically rolled back if an error occurs or by calling `Rollback()`:
Rolling Back a Transaction

To manually roll back a transaction, call the `Rollback()` method on the transaction object:
```csharp using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlTransaction transaction = conn.BeginTransaction()) { try { // Place transactional operations here, e.g., executing multiple SQL commands // If any operation throws an exception, the entire transaction is rolled back throw new Exception("Purposefully throwing an exception for rollback demo."); } catch (Exception ex) { // Log the error or perform any necessary cleanup Console.WriteLine(ex.Message); // Rollback the transaction transaction.Rollback(); } } } ```
In this example, an exception is intentionally thrown to demonstrate the rollback process. If a real error occurs, the transaction is automatically rolled back without needing to call `Rollback()` manually.
Committing a Transaction

To permanently save the changes made during a transaction, call the `Commit()` method on the transaction object:
Committing a Transaction



















Let's modify the previous example to demonstrate committing a transaction after successful operations:
```csharp using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlTransaction transaction = conn.BeginTransaction()) { try { // Place transactional operations here, e.g., executing multiple SQL commands // If all operations are successful, commit the transaction transaction.Commit(); } catch (Exception ex) { // Log the error or perform any necessary cleanup Console.WriteLine(ex.Message); // Rollback the transaction, if committing isn't possible transaction.Rollback(); } } } ```
Now that we've gone through basic SQL transaction examples in C#, you're well-equipped to handle database operations that require transactional integrity, ensuring data consistency and reliability in your applications.
To further explore SQL transactions in C#, consider reading Microsoft's official documentation or experimenting with more complex scenarios, such as using `SqlDataReader` or `SqlBulkCopy` within transactions. Happy coding!