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(); // 事务的回滚

相关文章:

  • 2022-12-23
  • 2021-07-14
  • 2021-09-26
  • 2022-01-16
  • 2022-01-01
  • 2021-11-26
  • 2021-07-30
  • 2021-08-18
猜你喜欢
  • 2021-06-22
  • 2021-10-08
  • 2021-09-12
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案