dir.by  
  Search  
Programming, development, testing
ADO.NET (working with the database on C#)
Transactions in ADO.NET | Database in the C# console application
  Looked at 5236 times    
 Transactions in ADO.NET | Database in the C# console application 
last updated: 22 July 2025
Transactions are used to execute the sequence of operations (to execute one operation after another).
If an error occurs during the execution of an operation, then all previous operations are rolled back (canceled).
That is, either all operations will be performed without errors or no operations will be performed (if at least one error has occurred).

Transaction Management:
Action
Example
 
Creating a Transaction
SqlTransaction transaction = connection.BeginTransaction();
 
Database Operation 1


Database Operation2


...
command.CommandText = "INSERT INTO Books (Name, Price) VALUES('On the sea', 120)";
command.ExecuteNonQuery();

command.CommandText = "INSERT INTO Books (Name, Price) VALUES('Reading the book', 230)";
command.ExecuteNonQuery();

...
 
Commit (succeed) of a transaction
transaction.Commit();
 
Rollback (cancellation of a transaction)
transaction.Rollback();
Step 1. Creating a new application
Step 2. Let's write the code
  C#     Write the code in the file Program.cs
using System;

// for SQL ADO.NET
using Microsoft.Data.SqlClient;

namespace ConsoleApplication1
{
     class Program
     {
          static void Main(string[] args)
          {
               // connection string
               string connectionString = @"Data Source=EVGENI;Initial Catalog=MyDatabase1;Integrated Security=True";

               // connect to db
               using (SqlConnection connection = new SqlConnection(connectionString))
               {
                    // open db
                    connection.Open();

                    // Creating a Transaction
                    SqlTransaction transaction = connection.BeginTransaction();

                    SqlCommand command = connection.CreateCommand();
                    command.Transaction = transaction;

                    try
                    {
                         // Operation 1
                         command.CommandText = "INSERT INTO Books (Name, Price) VALUES('На море', 120)";
                         command.ExecuteNonQuery();

                         // Operation 2
                         command.CommandText = "INSERT INTO Books (Name, Price) VALUES('Читаем книгу', 230)";
                         command.ExecuteNonQuery();

                         // Completing the Transaction
                         transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                         // Canceling a Transaction
                         transaction.Rollback();
                    }
               }
          }
     }
}
 
← Previous topic
Stored procedures in ADO.NET | C#
 
Next topic →
Read the image from the database and save it to a file | ADO.NET, C#
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What is ADO.NET?
Let's create a ADO.NET console application (C#, database)
Working with data in ADO.NET
Read the data (select) from the Database using SqlCommand and SqlDataReader in ADO.NET C#. Database Type Mapping
Read (select) data from the Database using SqlDataAdapter and DataSet | ADO.NET, C#
Get a scalar (single) value: Count, Min, Max, Sum, etc. | ADO.NET, C#
Add data (insert into) to the Database using SqlCommand | ADO.NET, C#
Change the data (update) in the Database using SqlCommand | ADO.NET, C#
Delete data in the Database using SqlCommand | ADO.NET, C#
Stored procedures in ADO.NET
Stored procedures in ADO.NET | C#
Transactions in ADO.NET
Transactions in ADO.NET | Database in the C# console application
Reading the picture from the database ADO.NET
Read the image from the database and save it to a file | ADO.NET, C#
Additional topics, questions
Database connection string | ADO.NET, C#

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика