using System.Transactions; using System.Data.SqlClient; using (TransactionScope scope = new TransactionScope()) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Perform database operations here // Abort the transaction scope.Transaction.Abort(); } }
using System.Transactions; using System.Messaging; using (TransactionScope scope = new TransactionScope()) { using (MessageQueue queue = new MessageQueue(queuePath)) { queue.Send(message); // Abort the transaction scope.Transaction.Abort(); } }In this example, a transaction is created using the TransactionScope class. Inside the transaction, a message is sent to a message queue using a MessageQueue object. If an error occurs or a condition arises that requires the transaction to be aborted, the Abort method is called to roll back any changes made during the transaction. Package/Library: System.Messaging, System.Transactions