Configuring SQL Server Transaction with ADO.NET

xiaoxiao2021-03-06  54

Configuring SQL Server Transaction with ADO.NET

Author: Builder.comTuesday, September 7 2004 4:59 PM

ADO.NET provides all functions required to process background data, and use as simple as reading a data from a table. Transaction (Transaction) Allows you to return database operations to a group, thereby ensuring that all operations can be performed. Because one of the operations fail, the entire transaction will fail. Let us now take a look at how to use transaction under the .NET Framework.

The transaction processing profile transaction is to combine a set of operations into a logical work unit. In the case where there is no error in the system, developers can use transaction to control and maintain continuity and integrity of each action in transaction processing.

Using such a method may result in developing two extreme conditions: either perform execution in all operations in transaction processing, or no operation is performed. This method is very necessary for real-time applications.

Banking is an example. Transaction processing should include a transfer process from an account to another account. This process belongs to transaction processing, the reason is that the expenditure from an account and the two actions in another account must be performed as a whole - any party does not allow failure. Before studying ADO.NET programming, let's take a look at how to perform transaction in SQL.

SQL transaction processing SQL allows developers to use two simple declarations to use transaction processing

Begin Transaction (Start Transaction)

Commit transaction (submit transaction)

All statements in two statements become part of transaction processing. Command Begin Transaction is at the starting position of the entire transaction, so all the subsequent commands will only be performed in one when the Command CommTration is executed. The ADO.NET method is as simple as.

Transaction Process requires a database connection and a transaction object. The difficulty of using transaction in SQL Server and ADO.NET is that the SQLTransaction class. There will be some changes in this type of database platform. For example, for the OLEDB database, the transaction process class name is OLEDBTRANSACTION.

System.Data.sqlclient Namespace includes the SQLTransaction class. This type includes two properties:

Connection: Indicates the SQLConnection object associated with transaction processing;

IsolationLevel: Defines the ISOLATIONLEVEL of transaction processing.

Properties ISOLATIONLEVEL is an enumeration object that includes the following members:

Chaos: PENDING CHANGES that appears from highly independent transactions cannot be covered;

Readcommitted: When the data needs to be read, use shared locks, but data can still be updated at the end of transaction processing, which causes non-repeatable data reads or Phantom Data. produce;

ReadunCommitted: Malicious reading data is possible, which means that there is no shared locks and does not achieve exclusive locks.

REPEATABLEREAD: Locks all the data used in the query, thereby avoiding other users to update the data. In the state of PHANTOM ROWS, this avoids non-repetitive data reads;

SerialISable: The range lock in the DataSet, thereby preventing other users from updating data in the end of the transaction, inserting the line in the database; isolationlevel defines the level of locking records, but this concept is not within the scope of this article. Object SQLTransaction also provides a similar way. You can use the following methods to perform transaction:

CommT: Submit database transaction;

Rollback: ROLL back transaction processing from the pending state. This operation cannot be performed once it is submitted.

Save: Creating SavePoint in transaction processing can reverse a part of transaction processing and specify a SavePoint name.

The following C # examples integrate these parts.

This simple console program will insert the two lines into the form of the Northwind database by following these steps:

Call the Begintransaction method of the Connection object to mark the starting position of the transaction. The BeGintransaction method returns a coordinate for transaction, which is assigned to the Command object used to transaction.

Specify the Transaction object to the Transaction property of the Command to be executed. If a Command is executed on the Connection in the active Transaction, and the Transaction object has not been specified to the transaction property of Command, an exception is generated.

Call the TRANSACTION object's commit method to end transaction processing, or call the Rollback method to cancel transaction.

The equivalent VB.NET code is similar.

The end of the transaction end Although this is a simple example, but it is still fully displayed how simple is to use transaction processing in .NET applications. Keep in mind that transaction is only necessary when processing a set of commands.

-------------------------------------------------- ------------------------------ author: Tony Patton to application developers as a starting point began his technology career. The Java, VB, Lotus, and XML-related certification he obtained have greatly improved his technical level.

转载请注明原文地址:https://www.9cbs.com/read-117147.html

New Post(0)