Using .NET Framework transaction of: ZDNET www.ASPCool.com Time: 2002-5-21 17:44:09 Views: 4636
After using ADO.NET for a while, you will find the flexibility between the new DataSet and Ado Recordset. Move forms and their relationship between layers and layers, and the ability to bind them to visual objects, and can use a set of standard classes to handle them is indeed .NET Framework unique. However, most business applications eventually need to insert, update, or delete data in the backend database. When these operations occur, you need to have a method to ensure the integration of the data. This is the reason for the event. Most developers understand the basic concept of transaction processing and a transaction ACID attribute. Fundamentally, you need to make sure that all modifications of the database should be completed during a specific transaction; otherwise they will have problems. There are two basic methods for managing database transactions in .NET: either through an ADO.NET Connection object or through a COM object. The Connection objects that use the ADO.NET Connection object system.data.sqlclient and System.Data.OleDbclient namespace support transaction semantics. For example, you like this can manage a transaction context database connection: Dim myConnection As New SqlConnection (myConnString) myConnection.Open () Dim myCommand As New SqlCommand () Dim myTrans As SqlTransaction 'Create the transaction context myTrans = myConnection.BeginTransaction ( later) 'Assign both transaction object and connection' to Command object for the transaction myCommand.Connection = myConnection myCommand.Transaction = myTrans performing any database to modify the code, you can use myTrans.Commit () commit the transaction process or use myTrans. The rollback () command rolls back to these changes. The transaction context using the Connection object has a serious limit, that is, it is only limited to only a single database (that is, the database you open). Of course, you don't need to complete the database operation from your own program, you can use the stored procedure of a specific database, this process contains the transaction code. Although this can reduce your processing time, you will still be restricted to execute a transaction process in a database. Controlling the transaction on multiple databases To control the transaction procedures on multiple databases, you will have to use the business mechanism provided by COM . The System.EnterpriseServieces object provides a method of operating a COM service through a unified namespace within the .NET. Part of the namespace includes the ability to manage transactions. To create this type of transaction, you must first create an object, this object inherits the function of the ServicedComponent object from the System.EnterpriseServices namespace. Your new object then can create and manage its own transaction context and use any resources with the Transaction Explorer. Therefore, you can not only manage transactions between two SQL Server databases and can also be between SQL Server and an Oracle database. The following code snippet demonstrates a simple transaction process between two databases.