using System.Transactions; using (var scope = new TransactionScope()) { // transaction code here Transaction.Current.TransactionCompleted += (object sender, TransactionEventArgs e) => { if (e.Transaction.TransactionInformation.Status == TransactionStatus.Committed) { // commit code here TransactionManager.Commit(); } } scope.Complete(); }
using System.Transactions; using System.Data.SqlClient; var connectionString = "Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True"; using (var scope = new TransactionScope()) using (var connection = new SqlConnection(connectionString)) { connection.Open(); // perform database operations here scope.Complete(); } TransactionManager.Commit();In this example, a transaction scope is created, and a SqlConnection object is opened inside the scope. Some database operations are executed, and if the transaction is completed, the TransactionManager.Commit method is called to commit the transaction to the database. Package/Library: System.Transactions, System.Data.SqlClient.