.NET SqlTransaction

  • 93
  • 0

.NET SqlTransaction 

            using (SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["trdataConnectionString"].ToString()))
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                SqlTransaction tran = conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandTimeout = 60;
                cmd.CommandText = "UPDATE AA SET AA=null WHERE (1=2)";
                cmd.Transaction = tran;
                int result = cmd.ExecuteNonQuery();

                if (result == 0)
                {
                    tran.Rollback();    //交易取消
                }
                else
                {
                    tran.Commit();      //執行交易
                }
                conn.Close();
            }

資料來源:

https://coolong124220.nidbox.com/diary/read/8045547