事务的使用。。

SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
myConnection.Open();

SqlTransaction myTrans = myConnection.BeginTransaction(); //使用New新生成一个事务
SqlCommand myCommand = new SqlCommand();
myCommand.Transaction = myTrans;

try
{
myCommand.CommandText = "Update Address set location='23 rain street' where userid='0001'";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Record is udated.");
}
catch(Exception e)
{
myTrans.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Sorry, Record can not be updated.");
}
finally
{
myConnection.Close();
}

 

 

 

我的应用 。。

 OracleCommand comm = null;
        OracleTransaction OT = null;//事物回滚  

// 定义事务 进行回滚。。
                        string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        OracleConnection sconn = new OracleConnection(connString);
                        sconn.Open();
                        OT = sconn.BeginTransaction();
                        comm = null;
                        comm = sconn.CreateCommand();
                        comm.Transaction = OT;

  comm.CommandText = "  DELETE FROM  t_fwzc WHERE FWZC_ID = '" + guid + "'";
                            comm.ExecuteNonQuery();
 OT.Commit();//事务进行提交
                        Response.Write("<script>alert('删除成功!');location ='index.aspx'</script>");

//用try。。。。catch 在catch中 写   OT.Rollback(); // 事务的回滚

你可能感兴趣的:(事务)