7.1. Using Distributed Transactions
Problem
You need to create a distributed transaction—a transaction that spans two or more data sources.
Solution
Use the TransactionScope
class within the System.Transactions
namespace.
The solution uses a table named SystemTransaction
in the AdoDotNet35Cookbook
database. Create the database in the AdoDotNet35Cookbook
database in both SQL Server and SQL Server Express instances by executing the following T-SQL statement.
USE AdoDotNet35Cookbook GO CREATE TABLE SystemTransaction( Id int NOT NULL PRIMARY KEY, Field1 nvarchar(50) NULL, Field2 nvarchar(50) NULL )
Tip
You need to configure the Microsoft Distributed Transaction Coordinator (MS DTC) in Windows to let applications enlist resources in a distributed transaction.
Follow these steps to fully enable MS DTC:
Select Control Panel→Administrative Tools→Component Services to open the Component Services dialog.
Expand Console Root→Component Services→Computers→My Computer in the tree view.
Right-click My Computer and select Properties from the context menu.
Select the MSDTC tab in the My Computer Properties dialog.
Click the Security Configuration button to open the Security Configuration dialog.
Ensure that all of the checkboxes are checked.
Ensure that the DTC Logon Account name is NT Authority\NetworkService.
Click OK to close the Security Configuration dialog.
Click OK to close the My Computer Properties dialog.
Close the Component Services dialog.
The solution successfully inserts a record into the SystemTransaction ...
Get ADO.NET 3.5 Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.