For Microsoft Visual Basic .NET versions of this article, see
315707.
This task content
Summary
Require COM Transaction Service Complete Code List Confirm that it can use troubleshooting reference
Summary This article describes how to use COM (Component Services) transactions in the Visual C # .NET class. A set of database operations is considered a transaction unit. Either all operations are successful; or if one of the operations fails, the entire transaction failed. For the latter case, any database operations attempted will not be released into the underlying database.
Back to top
Requirements the following describes the recommended hardware, software, network facilities, skills and knowledge and the necessary service pack:
Microsoft Windows 2000 Server SP1 Microsoft Internet Information Services (IIS) 4.0 or later Microsoft Internet Explorer 5.0, 5.5 or 6.0 This article assumes that you are familiar with the following topics:
Concept of transaction and processing COM (component service)
Back to top
COM Transaction Service You can use Microsoft .NET Framework
System.EnterpriseServices namespace to implement transaction processing. To access the COM transaction service, you need to create a class. To do this, follow these steps:
Start Visual Studio .NET. On the File menu, point to "New" and click Project. Click "Visual C # item" under Project Type, then click "Class Library" under Template. Name the project prjenterprise. Class1 will be created by default. In the Solution Explorer, right-click "Reference" and click Add Reference. The Add Reference dialog will appear. Double-click "System.EnterpriseServices" under the "Component" tab of the .NET tab. Make sure "System.EnterpriseServices" is displayed under "selected components". Click OK. Adding the following code to any other statement in the class1.cs file: use system.enterprisservices;
Using system.data.sqlclient; add a new class named Clsw to the Class1.cs file. To use the COM transaction service, your class (CLSES) must be inherited from the serviceDcomponent in the following: PUBLIC CLSS CLSES: ServicComponent uses a transaction property as follows: [Transaction (TransactionOption.Required) ] public class clses: ServicesDComponent creates a method in the Clity class, then named DBACCESS, which receives four integers as input parameters. The first two parameters provide a product ID and the quantity of this product; the latter two parameters provide a product ID and the stock of this product. This method performs a set of database operations on these specified product IDs, which will be considered a transaction. Void DBACCESS (int PID1, INRDER, INT PID2, INT Instock) In the DBACcess method, create a SQL connection object for the Northwind database and open the connection. Database operations are performed using the following database: Note Don't forget to change the following connection string parameters to reflect the correct value of your SQL Server server. SqlConnection conn = new sqlConnection ("User ID =
SQLCommand.executeNonQuery (); performs another update to the Products table. Update the UnitsStock field with the product with the specified ID using the instock value according to the specified of the third and fourth input parameters. Run this SQL update using the following SQL command: SQLCommand.commandtext = "Update myproducts set unitsinstock =" instock "where produter =" PID2;
Sqlcommand.executenonquery ();
Because these updates are part of the COM transaction process, they are submitted as a unit. If there is no incorrect error, you will submit this transaction using the setComplete method of the ContextUtil class in the System.EnterpriseServices namespace (two updates in this example): ContextUtil.setComplete (); connection with the Northwind database Close: Conn.Close );} You must capture any exceptions that occur when running the SQL command, so you can terminate the entire transaction: Catch (Exception E) {Terminate the setAbort method of the ContextUtil class in the System.EnterpriseServices namespace to terminate the entire transaction. If the first update is successful but the second update fails, the two updates will not be released to the Products table. The captured exception will throw to the caller, indicating that the transaction has failed: contextUtil.seTabort ();
Throw e;
} In order to make this component correctly, the component must have a strong name. Generate a strong name and then use this strong name to sign the assembly. To do this, follow these steps:
At the Visual Studio .NET command prompt, type sn.exe -k snenterprise.snk to create a key file. For more information on using the Strong Name to sign the assembly, see the .NET Framework SDK documentation. Copy NEnterprise.snk into your project folder. In AssemblyInfo.vc, add the following code before or after the following: [Assembly: assemblykeyKeyAttribute ("..//// snenterprise.snk)] Save, and then generate your project.
Back to top
Complete code list
Note Don't forget to change the following connection string parameters to reflect the correct value of your SQL Server server.
Using system;
Using system.data;
Using system.data.sqltypes;
Using system.data.common;
Using system.enterprises;
Using system.data.sqlclient;
Namespace PrjEnterprise
{
[Transaction (TransactionOption.Required)] Public Class Clses: ServicesDComponent
{
Public SqlConnection CONN;
Public Void Dbaccess (int PID1, int Onorder, int PID2, int Instock)
{
Try
{
SqlConnection conn = new sqlconnection ("User ID =
Cn.open ();
Sqlcommand sqlcommand = new sqlcommand ("Update MyProducts Set Unitsonorder =" Onorder "Where ProductId =" PID1, CONN);
Sqlcommand.executenonquery ();
Sqlcommand.commandtext = "Update myproducts set unitsinstock =" instock "where produter =" PID2; sqlcommand.executenon query ();
Contextutil.setComplete ();
CONN.CLOSE ();
}
Catch (Exception E)
{
Contextutil.Setabort ();
Throw e;
}
Finally
{
}
}
}
}
Back to top
Confirm that it can be used to test this code, you need to create a console application using the CLSES project. One situation is: transaction success, designated product
Onorder and
The Instock field has been updated. Another situation is: for a designated product
The update of the onORDER field is successful, but for another designated product
The update of the Instock field failed (because the specified product number is not present in the ProduCTS table). This will result in failure of the transaction, which will be ignored.
In Visual Studio .NET, point to "New" on the File menu, and then click Project. Click "Visual C # item" under Project Type, and then click Console Application under Template. In the Name text box, type Testes. Make sure the Add Solutions option has been selected. Click OK to add the item to the solution. To make Testes test clses, you must add a reference. In the Solution Manager, right-click References under Testes (you just added), then click Add Reference. The Add Reference dialog box appears. In the Project tab, double-click "Prjenterprise". Display a reference under "Selected Components". Click OK to add a reference to the item. Add a reference to the System.EnterpriseServices library in your project. In the Solution Explorer, right-click "Reference" and click Add Reference. The Add Reference dialog will appear. Double-click "System.EnterpriseServices" under the "Component" tab of the .NET tab. Make sure "System.EnterpriseServices" is displayed under "selected components". Click OK. Right-click Console Application (Testes), and then click Set to Start Item. Paste the following source code into the main function of the Class1 class: prjenterprise.clses mytest = new prjenterprise.clses ();
Try
{
Mytest.dbaccess (1, 777, 2, 888);
Console.writeline ("Transaction One - Success";
Mytest.dbaccess (1, 5, 2, -20);
Console.writeline ("Transaction Two - Success");
}
Catch (Exception E)
{
Console.Writeline ("Transaction Failure);
} Press the F5 key to run the test code. In step 7 of the code, the first call for DBACCESS is successfully completed. Product 1 and Product 2 are all in the Products table. Product 1 ONORDER field is updated to 777, and the Instock field of product 2 is updated to 888. Because this transaction is completed successfully, you will receive the following message in the output window.
Transaction One - Success failed to DBACCESS. Therefore, the DBACCESS will not be published to the database from any update statement of the Products table. Although product 1 can be updated to 5, product 2 cannot set its instock field to -20 (because a constraint condition defined in the Products table definition specifies that the Inelling value is not allowed). Therefore, this call to DBACCESS failed, so that the entire transaction operation also failed. The Products table will keep the status before calling DBAccess. Catch statement handles transaction failed notifications from DBACCESS, you will receive the following error message in the output window:
Transaction Failure
Use the SQL Server Enterprise Manager to check the contents of Northwind's Products table. View product 1, the value of the onORDER field is 777. View Product 2, the value of the Instock field is 888. It can be seen that the second call to DBAccess (this callbook will make these fields have different values) fail.
Back to top
Troubleshooting
Make sure all items using the COM service have a strong name. All classes using COM services must inherit the service component. Service components are located in the System.EnterpriseServices namespace. When commissioning, the transaction may time out before committing or termination. To avoid timeout, use a timeout attribute in the transaction property. In the following example, when any transaction is completed, the associated method has 1,200 seconds before the timeout. [Transaction (TransactionOption.Required, Timeout: = 1200)]]
Back to top
Refer to additional information, please visit the following Microsoft Developer Network (MSDN) Web site:
COM Integration: .Net Enterprise Services how to help you generate distributed applications http://msdn.microsoft.com/msdnmag/issues/01/10/complus/default.aspx
Service components: http://msdn.microsoft.com/library/default.asp? Url = / library / en-us / f_and_m / html / vxconinterop.asp
Back to top
The information in this article applies to:
Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002)
Recent Updated: 2003-12-23 (2.0) Keywords: KBComplusobj KBSQLPROG KBSQLCLIENT KBDATABASE KBHOWTOMASTER KB816141 KBAUDDEVELOPER