Udostępnij za pośrednictwem


System.Transactions Sample

The System.Transactions sample demonstrates controlling transactions by using the managed APIs located in the System.Transactions namespace. In particular, the System.Transactions.TransactionScope class is used to establish a transaction boundary to ensure that inventory figures are not adjusted unless there is sufficient inventory to cover the request, and if there is sufficient inventory that the transfer from of the inventory from one location to another occurs in an atomic fashion. Automatic registration in a distributed transaction is demonstrated by logging changes in inventory to an auditing database stored on a separate server.\

This sample must be installed in two parts on two physical machines: a main sample server and an audit server.

Install directory: drive:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\Transaction\

Scenario

Jane is a developer for Adventure Works Cycles. She needs to track inventory movements from one location to another in a transaction consistent manner, and participate in auditing any inventory changes.

Languages

Transact-SQL, Visual C# and Visual Basic.

Features

The System.Transactions sample uses the following features of SQL Server.

Application Area Features

Overall

Common language runtime (CLR), Transactions, Distributed Transactions, ADO .NET, MSDTC

Prerequisites

Before running this sample, make sure the following software is installed:

  • Microsoft SQL Server 2005 or Microsoft SQL Server 2005 Express Edition (SQL Server Express). You can obtain SQL Server Express free of charge from the SQL Server 2005 Express Edition Documentation and Samples Web site
  • The AdventureWorks database which is included with SQL Server 2005, and is also available at the SQL Server Developer Web site.
  • The SQL Server 2005 Database Engine samples. These samples are included with SQL Server 2005. You can download the latest version of the samples at the SQL Server Developer Web site.
  • .NET Framework SDK 2.0 or Microsoft Visual Studio 2005. You can obtain .NET Framework SDK free of charge. See Installing the .NET Framework SDK.
  • Microsoft Distributed Transaction Coordinator (MSDTC). MSDTC must be configured to permit inbound and outbound client connections. You must install and configure MSDTC on both servers used in this sample. For more information, see the Component Services documentation.

Before running this sample, configure the following components:

  • If Windows Firewall is enabled on either server, configure it on both servers to allow for SQL Server and MSDTC connections. For more information, see the Windows Firewall documentation.
  • Use the SQL Server Surface Area Configuration tool to enable remote access for SQL Server 2005 on the audit server.

Building the Sample

If you have not already created the strong-name key file ExternalSampleKey.snk, generate the key file by using the following instructions.

To generate a strong name key file

  1. Open a Microsoft Visual Studio 2005 command prompt. Click Start, point to All Programs, point to Microsoft Visual Studio 2005, point to Visual Studio Tools, and then click Visual Studio 2005 Command Prompt.

    —Or—

    Open a Microsoft .NET Framework command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

  2. At the command prompt, use the change directory (CD) command to change the current folder of the command prompt window to the Samples folder.

    Note

    To determine the folder where samples are located, click Start, point to All Programs, point to Microsoft SQL Server 2005, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\90\Samples.

  3. At the command prompt, run the following command to generate the key file:

    sn -k ExternalSampleKey.snk

    Important

    For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" in the .NET Development Center on MSDN.

To build the System.Transactions sample

  1. Edit CS\Transaction\InventoryMover.cs, and change the value of the string constant auditConnectionString to correctly name the server that will store the auditing information for this sample. This server must be a different physical computer from the one that you will use to install the main part of this sample. Set up a test account with SQL Server Authentication on the audit server, and then modify the auditConnectionString to specify the user name and password you gave to this account.

  2. Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution, or by using Microsoft MSBuild, which is included in the .NET Framework SDK 2.0. Run a command similar to the following at a command prompt:

    msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\Transaction.sln

  3. Make sure that the AdventureWorks database is installed.

  4. If you did not install the SQL Server engine samples in the default location, modify the path in the CREATE ASSEMBLY portion of the script in Scripts\InstallCS.sql and Scripts\InstallVB.sql to refer to the location where the samples were installed.

  5. If you are not an administrator for the SQL Server instance you are using, you must have an administrator grant you CreateAssembly permission to complete the installation.

  6. Open the scripts\installCS.sql or scripts\installVB.sql file, depending on whether you compiled the Visual C# project or the Visual Basic project, in Microsoft Management Studio. Run the script that is contained in the file while you are connected to the main sample server, or run a command similar to the following in a command prompt window on the main sample server:

    sqlcmd -E -I -i Scripts\InstallCS.sql

  7. Open the scripts\installDB.sql file in Microsoft Management Studio. Run the script that is contained in the file while you are connected to the audit server, or run a command similar to the following in a command prompt window on the audit server:

    sqlcmd -E -I -i Scripts\InstallDB.sql

Running the Sample

To run the System.Transactions sample

  1. Open the scripts\test.sql file in Management Studio. Run the script that is contained in the file, or run the following command in a command prompt window:

    sqlcmd -E -I -i Scripts\test.sql

Removing the Sample

To remove the System.Transactions sample

  1. Open the scripts\cleanup.sql file in Management Studio. Run the script that is contained in the file on the main sample server, or run the following command in a command prompt window on the main sample server:

    sqlcmd -E -I -i Scripts\cleanup.sql

  2. Open the scripts\cleanup.sql file in Management Studio. Run the script that is contained in the file on the audit server, or run the following command in a command prompt window on the audit server:

    sqlcmd -E -I -i Scripts\cleanupDB.sql

Comments

The CLR for SQL Server 2005 or SQL Server Express must be enabled for this sample to work correctly.

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples. Sample applications and assemblies should not be connected to or used with your production SQL Server database or your report server without the permission of the system administrator.