Asp. The transaction processing in net

Source: Internet
Author: User
Tags commit insert one table reference rollback
A asp.net| transaction transaction is a set of database operations that synthesize logical units of work, although errors may occur in the system, but transactions control and maintain the consistency and integrity of each database. If no errors are encountered during the transaction, all changes in the transaction are permanently part of the database. If an error is encountered, no modifications are made to the database.

For example, in a banking application, if funds are transferred from one account to another, a certain amount is credited to one account and the same amount is debited to another account. Because computers may fail due to power outages, network outages, and so on, it is possible to update rows in one table, but not to update rows in related tables. If the database supports transactions, you can group database operations into a transaction to prevent inconsistencies in the database due to these events.


In Ado.net, you can use Connection and Transaction objects to control transactions. To perform a transaction, follow these steps:


Invokes the BeginTransaction method of the Connection object to mark the start of the transaction. BeginTransaction returns a reference to Transaction. Keep this reference so that it can be assigned to the Command that is enlisted in the transaction.

Assigns the Transaction object to the Transaction property of the Command to be executed. An exception is thrown if the command is executed against Connection through the active Transaction object, but the Transaction object has not been assigned to the command's Transaction property.

Execute the required commands.

Call the Transaction object's Commit method to complete the transaction, or call the Rollback method to cancel the transaction.

The following code example uses Microsoft? SQL Server? Ado.net to demonstrate the transaction logic.


SqlConnection myconnection = new SqlConnection ("Data source=localhost;initial catalog=northwind;integrated Security= SSPI; ");
Myconnection.open ();
Start a transaction
SqlTransaction Mytrans = Myconnection.begintransaction ();


Create a command for a transaction
SqlCommand mycommand = new SqlCommand ();
Mycommand.connection=myconnection;
Mycommand.transaction = Mytrans;
Try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (+," Description ")";
Mycommand.executenonquery ();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (" Description ")";
Mycommand.executenonquery ();
Mytrans.commit ();
Console.WriteLine ("Both records are written to database.") ;
}
catch (Exception e)
{
Mytrans.rollback ();
Console.WriteLine (E.tostring ());
Console.WriteLine ("Neither record is written to database.") ;
}
Finally
{
Myconnection.close ();
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.