ADO.Net 2.0 Transaction Processing
A lot of
new features have been included in ADO.Net 2.0 and we will be discussing
transactions in this article. Transactions are an important part in any
database applications development.
Transactions processing is such an area and any improvement in that area is definitely going to make your application robust and stable. The transactions can be distributed or local. Implementing a series of task and committing the task if all the tasks were successful or else if any of the tasks fail the whole series of tasks is rolled back is called a transaction. This is how most of the related tasks are done with respect to a database application. It is also possible to write your transaction code in a stored procedure and then invoke the stored procedure. A developer can make use of the TransactionScope object while working with transactions.
We will see a simple transaction which uses the TransactionScope object to understand how this object is used.
transScope = new System.Transactions.TransactionScope())
In the above code we are creating a new TransactionScope object and writing all the transaction processing code within a block that comes under the scope of the TransactionScope. In the above code the SQL connection itself comes under the TransactionScope and any error in the connection itself will not commit the transactions done. To commit or rollback the transactions the Consistent property of the TransactionScope object is set to either true or false. You can also enclose the cmdObjs ExecuteNonQuery() statement in a try/catch block to write an improved version of the above code.
Consider a scenario where you are required to update two databases and both the databases have to be updated or none of the databases should be updated. In such cases you will find the use of TransactionScope object very useful. The only thing you need to do is to enclose all the transaction processing in a block that comes under the TransactionScope object. The code given below gives you an idea on how to go about in that scenario.
In the above code the second line of the code creates the TransactionScope object and all the other codes are written within the scope of the TransactionScope object in a block that comes under it. In this code we are using two connection objects namely conn1 and conn2 for two separate commands to be executed in two different databases. Once all the process in the try block is executed we are setting a Boolean variable transConsist to true. This variable is used to commit or rollback the transactions by setting up the TransactionSope objects Consistent property in the last line of the code as transScope.Consistent = transConsist;.
Thus we find that using the TransactionScope object is a very easy method to deal with transactions in ADO.Net 2.0. The transaction promoted to a distributed transaction mode when we try to access another database for completing the transaction. This conversion to the distribution transaction mode of the local mode is done automatically. Hence is it effective and easy to use the TransactionScope object for transaction processing.
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.
Copyright - © 2004 - 2023 - All Rights Reserved.