Transaction transaction processing in C #

Source: Internet
Author: User
Tags rollback

Definition: A task (Transaction) is a unit of control and a sequence of operations defined by the user. All of these operations Yao did, or did not do, is an indivisible work unit. Through tasks, SQL Server can bind the logical group of operations together so that the servers maintain the integrity of the data. SqlConnection conn=new SqlConnection (str); Link db
Sqlcommand Cmd=conn.   CreateCommand (); Create a SQL command image
SqlTransaction ston; Create a task for the elephant
Conn.Open (); Open a link
Ston=conn.begintransaction (); Start a task
Cmd.    Transaction=ston; To assign the things that happen to you
Try
{

Cmd.executenonquery ();

Cmd.       ExecuteNonQuery ();    ... ston.commit (); Submit a Task
}
catch ()
{
Ston.    RollBack (); Things to roll
Example: protected void Page_Load (object sender, EventArgs e)
{SqlConnection conn = new Sqlconnectio ("server=10.158.17.22//litianxiang;uid=sa;pwd=tianxiang;database=test;");
Conn. Open ();
SqlTransaction TRAN = conn. BeginTransaction ();
SqlCommand cmd = conn. CreateCommand ();
Cmd.             Transaction = Tran; Try
{
Cmd.commandtext = "INSERT suggestion ([Content],reply,isanonymous,lasteditby,lasteditdt) VALUES (' Litianxiang ', ' Litianxiangreply ', 1, ' Litianxiang ', GETDATE ()) ";
Cmd.                   ExecuteNonQuery (); Cmd.commandtext = "UPDATE suggestion SET content= ' Jiangyingliang ' where suggestionid= ' 11 '";
Cmd.                   ExecuteNonQuery (); Tran.commit ();
}
catch (Exception ex)
{
Tran. Rollback ();
This.lbl.Text = ex. message;
}
Finally
{
Conn. Close ();
}
}

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.