. NET development in the transaction processing of the big competition of the ASP. NET page-level transactions __.net
Source: Internet
Author: User
Asp. NET transaction can be said to be in. NET platform is the simplest way to implement the transaction, you need only one line of code. Add an extra attribute to the page declaration of ASPX, which is the transaction property
transaction= "Required", it has the following values: Disabled (default), notsupported, supported, required, and requiresnew, these settings are the same as those in COM + and Enterprise Services, and a typical example is if you want to run a transaction within the context of a page , then set it to Required. If the page contains a user control, the controls are also included in the transaction, and the transaction exists everywhere on the page.
code example:
/>/>/> Page declaration transaction= "Required":
<%@ Page
transaction= "Required"Language= "C #" autoeventwireup= "true"
Codebehind= "WebForm3.aspx.cs" inherits= "WEBAPPLICATION4.WEBFORM3"%>
Page reference: Using System.EnterpriseServices;.
Then, the data manipulation code:
protected void Button1_Click (object sender, EventArgs e)
{
Try
{
Work1 ();
WORK2 ();
ContextUtil.SetComplete (); //Commit a transaction }
catch (System.Exception except)
{
ContextUtil.SetAbort (); //undo Transaction Response.Write (except. message);
}
}
private void Work1 ()
{
String constring = "Data Source=127.0.0.1;database=codematic;user Id=sa;
Password= ";
SqlConnection myconnection = new SqlConnection (constring);
String strSQL = "Insert into p_category (categoryid,name) VALUES (' 1 ',
' Test1 ') ";
SqlCommand mycommand = new SqlCommand (strSQL, MyConnection);
Myconnection.open ();
int rows = Mycommand.executenonquery ();
Myconnection.close ();
}
private void Work2 ()
{
String constring = "Data Source=127.0.0.1;database=codematic;user Id=sa;
Password= ";
SqlConnection myconnection = new SqlConnection (constring);
String strSQL = "Insert into p_category (categoryid,name) VALUES (' 2 ',
' Test2 ') ";
SqlCommand mycommand = new SqlCommand (strSQL, MyConnection);
Myconnection.open ();
int rows = Mycommand.executenonquery ();
Myconnection.close ();
}
ContextUtil is the preferred class for obtaining COM + context information. Because members of this class are all static, this class does not need to be instantiated before using its members.
Asp. NET page transactions have the following advantages and limitations.
• Advantages: Simple to implement, no additional coding required.
L Restriction: All the code on the page is the same transaction, such a transaction can be very large, and perhaps we need a separate, small transaction implementation in the Web layer.
Selected from the Liang Jian. NET. . NET deep experience and the essence of actual combat "a book 5.4 section."
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.