This sample works only with SQL Server 2005 and SQL Server 2008. It will not work with any version of SQL Server earlier than SQL Server 2005.
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: C:\Program Files\Microsoft SQL Server\100\Samples\Engine\Programmability\CLR\Transaction\
SQL Server samples and sample databases must be downloaded and installed before you can view or work with them. For more information, see Considerations for Installing SQL Server Samples and Sample Databases
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.
Transact-SQL, Visual C# and Visual Basic.
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 |
Before running this sample, make sure the following software is installed:
- SQL Server or SQL Server Express. You can obtain SQL Server Express free of charge from the SQL Server Express Documentation and Samples Web site
- The AdventureWorks database that is available at the SQL Server Developer Web site.
- The SQL Server Database Engine samples that are available 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 Documentation.
- 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 Configuration Manager tool to enable TCP/IP or named pipe access for SQL Server 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
- Open a 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 .NET Framework command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.
- 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, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in C:\Program Files\Microsoft SQL Server\100\Samples\.
- 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
- 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.
- Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution, or by using 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
- Make sure that the AdventureWorks database is installed.
- 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.
- 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.
- 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 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
- Open the scripts\installDB.sql file in 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
- 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
- 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
- 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
The CLR for SQL Server 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.
© 2008 Microsoft Corporation. All rights reserved.