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 ();
}
}