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:

  1. Select Control Panel→Administrative Tools→Component Services to open the Component Services dialog.

  2. Expand Console Root→Component Services→Computers→My Computer in the tree view.

  3. Right-click My Computer and select Properties from the context menu.

  4. Select the MSDTC tab in the My Computer Properties dialog.

  5. Click the Security Configuration button to open the Security Configuration dialog.

  6. Ensure that all of the checkboxes are checked.

  7. Ensure that the DTC Logon Account name is NT Authority\NetworkService.

  8. Click OK to close the Security Configuration dialog.

  9. Click OK to close the My Computer Properties dialog.

  10. 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.