Perform transactions on Asp. Net data operations at three levels

Source: Internet
Author: User
Many database operations require transactions. There are roughly three levels of transactions under Asp.net:
(1) transactions at the stored procedure level
(2) Ado. Net transactions
(3) Asp. Net page-level transactions
The following are examples:
First, create the trantest table, field id (int), test (char)
Set a primary key for the id (transaction test is performed using the primary key feature that does not allow repeated requests)

(1)
Create procedure Tran1
As
Begin tran
Insert Into trantest (id, test) values (1, # test @#)
Insert Into trantest (id, test) values (1, # test @#)
If (@ error = 0)
Commit tran
Else
Rollback tran
GO
When this stored procedure is run, no record is found, indicating that it is indeed rolled back.
Clear database records and modify the Stored Procedure
Create procedure Tran1
As
Begin tran
Insert Into trantest (id, test) values (1, # test @#)
Insert Into trantest (id, test) values (2, @ # test @#)
If (@ error = 0)
Commit tran
Else
Rollback tran
GO
When this stored procedure is run, two records are added, indicating that the transaction is committed.
Clear database records

(2)
SqlConnection conn = new SqlConnection (System. Configuration. ConfigurationSettings. deleettings ["conn"]);
SqlCommand cmd1 = new SqlCommand ("Insert Into trantest (id, test) values (1, @ # test @ #)", conn );
SqlCommand cmd2 = new SqlCommand ("Insert Into trantest (id, test) values (1, @ # test @ #)", conn );
Conn. Open ();
SqlTransaction tran = conn. BeginTransaction ();
Statement 1.transaction = tran;
Export 2.transaction = tran;
Try
{
Statement 1.executenonquery ();
Listen 2.executenonquery ();
Tran. Commit ();
}
Catch (SqlException failed T)
{
Tran. Rollback ();
Response. Write (Response T. Message );
}
Finally
{
Conn. Close ();
}
No record is added for the same running result. After clearing the record, modify it
SqlCommand cmd1 = new SqlCommand ("Insert Into trantest (id, test) values (1, @ # test @ #)", conn );
SqlCommand cmd2 = new SqlCommand ("Insert Into trantest (id, test) values (2, @ # test @ #)", conn );
There are 2 records in the database after running

(3)
Add reference System. EnterpriseServices. dll
Using System. EnterpriseServices;

ServiceConfig config = new ServiceConfig ();
Config. Transaction = TransactionOption. Required;
ServiceDomain. Enter (config );
Try
{
Work1 ();
Work2 ();
ContextUtil. SetComplete ();
}
Catch (System. Exception doesn't)
{
ContextUtil. SetAbort ();
Response. Write (Response T. Message );
}
Finally
{
ServiceDomain. Leave ();
}

Then, add two operations to the page to simulate calling operations of different classes at the logic layer.
Private void work1 ()
{
SqlConnection conn = new SqlConnection (System. Configuration. ConfigurationSettings. deleettings ["conn"]);
SqlCommand cmd1 = new SqlCommand ("Insert Into trantest (id, test) values (1, @ # test @ #)", conn );
Conn. Open ();
Statement 1.executenonquery ();
Conn. Close ();
}

Private void work2 ()
{
SqlConnection conn = new SqlConnection (System. Configuration. ConfigurationSettings. deleettings ["conn"]);
SqlCommand cmd2 = new SqlCommand ("Insert Into trantest (id, test) values (1, @ # test @ #)", conn );
Conn. Open ();
Listen 2.executenonquery ();
Conn. Close ();
}

After the database is cleared and run, no records are written. Clear the database again and modify work2 ()
SqlCommand cmd2 = new SqlCommand ("Insert Into trantest (id, test) values (2, @ # test @ #)", conn); after running, two records are added.

NOTE 2:
1,
ServiceConfig config = new ServiceConfig ();
Config. Transaction = TransactionOption. Required;
ServiceDomain. Enter (config );
To the end of ServiceDomain. Leave (); indicates
A transaction is performed in this section. If you want to simply perform a transaction for the entire page, modify the transaction as follows:
Try
{
Work1 ();
Work2 ();
ContextUtil. SetComplete ();

}
Catch (System. Exception doesn't)
{
ContextUtil. SetAbort ();
Response. Write (Response T. Message );
}
In this way, do not forget to add
Transaction = "Required"
2. Do not add try {} catch {} code block in work1 () or work2 ().
3. Because my machine is xp sp2 and I didn't notice this problem, I suddenly realized that, with platform restrictions, it can only be used in ipvs2003 or xp xp2, otherwise, the error message "ServiceConfig is not supported on the current platform" will be displayed, and you will have the opportunity to test it again. To use the patch in xp sp1, refer:
Http://www.alexthissen.nl/Weblog/PermaLink.aspx? Guids = f6d61461-d336-40b0-9f4d-51eab6650f27
Http://www.rm.com/Support/GeneralDownload.asp? Cref = DWN222592 & nav = 0

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.