Transactions Technology Sample

This sample demonstrates how to use transactions in a .NET Framework application. It specifically illustrates the "commit" and "abort" functions of transactions by retrieving a value from a SQL Server database, allowing the user to modify the value, and posting the new value to the database.

Note

To build and execute this sample, you must have Microsoft SQL Server 2000 or later installed on your computer, and the database engine must be configured to use Windows Authentication. Other configurations require you to edit the SQL connection string in the TxObj.cs or TxObj.vb file.

For information about using the samples, see the following topics:

To create the TxDemoDB database

  1. Open a Command Prompt window and navigate to one of the language-specific subdirectories under the Technologies\ComponentServices\Transactions directory.

  2. Type osql -E -S (local)\SQLExpress -i TxDB.sql at the command line. Alternatively, open the SQL Server Query Analyzer window, load TxDB.sql, and choose Execute.

    This action creates a new TxDemoDB database in your SQL Server.

To build the sample using the Command Prompt

  1. Open a command prompt window, and navigate to one of the language-specific subdirectories under the Technologies\ComponentServices\Transactions directory, or use the same command window that you used to create the TxDemoDB database above.

  2. Type msbuild TransactionsCS.sln or msbuild TransactionsVB.sln, depending on your choice of programming language, at the command line.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories for the sample.

  2. Double-click the icon for TransactionsCS.sln or TransactionsVB.sln, depending on your choice of programming language, to open the file in Visual Studio.

  3. On the Build menu, click Build Solution.

To run the sample

  1. Open the Command Prompt window or Windows Explorer and navigate to the directory that contains the newly built executable file.

  2. Type TxDemo.exe at the command line, or double-click the executable's icon in Windows Explorer.

  3. The current value is read from the database and displayed.

  4. Enter a number equal to or greater than 0 and less than or equal to 10 in the New Value field, and click the Post button.

  5. The message "About to commit the transaction" should appear. Click OK. The Current Value field is now updated with the value that you entered.

  6. Enter a number that is less than 0 or greater than 10 in the New Value field. Click the Post button.

  7. The message "About to abort the transaction because the new value ([yourvalue]) is either <0 or >10." should appear. Click OK. Notice that the Current Value field does not change. This is because during the transaction the current value did change, but when the transaction was rolled back, the value was changed back to its original value.

  8. Now try the same data with the AutoPost button. You will notice that when the demo aborts the transaction it throws a COMException. This is to notify the AutoComplete to do an abort.

To uninstall the sample

  1. In the Command Prompt window, navigate to the directory where you ran msbuild.exe, or where you opened the .sln file.

  2. Type msbuild.exe TransactionsVB.vbproj /t:Clean or msbuild.exe TransactionsCS.csproj/t:Clean, depending on your choice of programming language. This action calls regsvcs.exe and gacutil.exe with the /u switch in order to remove the sample files from the COM+ catalog and the global assembly cache.

  3. Type osql -E -S (local)\SQLExpress -i TxDBClean.sql at the command line. Alternatively, open the SQL Server Query Analyzer window, load the TxDBClean.sql file, and choose Execute. This action removes the TXDemoDB database from your SQL Server.

Requirements

.NET Framework Version: 2.0, 1.1, 1.0

Remarks

A transaction is a set of operations that belong together. Either all of these operations or none should be performed. Whenever one of the operations in the transaction fails, all prior operations must roll back to ensure there is no inconsistency.

For more information about sample binary creation and registration steps, see the comments in the source code files. In the Component Services administrative tool, the C# component appears as TxDemoServerCS.TxObject. The VB version appears as TxDemoServerVB.TxObject.

See Also

Concepts

Local Transactions (ADO.NET)

Reference

AutoCompleteAttribute

ContextUtil

ServicedComponent

System.ComponentModel

System.Data

System.Data.SqlClient

System.Drawing

System.EnterpriseServices

System.Reflection

System.Runtime.InteropServices

System.Runtime.Serialization

System.Windows.Forms

TransactionAttribute

TransactionOption

WebMethodAttribute