zoukankan      html  css  js  c++  java
  • C#、SQL中的事务

    c#方法一:
           TransactionOptions transactionOption = new TransactionOptions(); //设置事务隔离级别 transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; // 设置事务超时时间为60秒 transactionOption.Timeout = new TimeSpan(0, 0, 60); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption)) { int id = 0; try {
                //do something
    scope.Complete(); } catch (Exception ex) { throw ex; } finally { scope.Dispose(); } }
     c#方法二:  SqlTransaction   sqlTransaction   =   sqlConnection.BeginTransaction();   
             SqlCommand   sqlCommand   =   new   SqlCommand();  
             sqlCommand.Transaction = sqlTransaction;
             sqlTransaction.Commit();  
             try   
              {   
                //   利用sqlcommand进行数据操作   
                  ...   
                //   成功提交   
                sqlTransaction.Commit();   
              }   
              catch(Exception   ex)   
              {   
                  //   出错回滚   
                sqlTransaction.Rollback();   
              }  
               finally   
                  {   
                        cnn.Close();   
                        trans.Dispose();   
                        cnn.Dispose();   
                   }    
      BEGIN TRANSACTION
      /*--定义变量,用于累计事务执行过程中的错误--*/
      DECLARE @errorSum INT
      SET @errorSum=0 --初始化为0,即无错误
      /*--转账:张三的账户少1000元,李四的账户多1000元*/
       
      SET @errorSum=@errorSum+@@error --累计是否有错误
         IF @errorSum<>0 --如果有错误
      BEGIN
      print '交易失败,回滚事务'
      ROLLBACK TRANSACTION        --回滚
      END
      ELSE
      BEGIN
      print '交易成功,提交事务,写入硬盘,永久的保存'
      COMMIT TRANSACTION        --执行修改保存
      END
      GO
      print '查看转账事务后的余额'
      GO
    SQL中

     参考: http://www.cnblogs.com/Garden-blog/archive/2011/04/21/2023417.html

  • 相关阅读:
    连续型随机变量
    离散型随机变量
    vue1.0生命周期
    vue2.0生命周期函数
    vue2.0 vue.set()
    vue2.0 vue.extend()的拓展
    vue2.0 自定义指令详解
    vue2.0 v-model指令
    vue2.0排序应该注意的问题
    vue2.0版本指令v-if与v-show的区别
  • 原文地址:https://www.cnblogs.com/woloveprogram/p/4682911.html
Copyright © 2011-2022 走看看