存储过程中的事务实现

       一直以为存储过程会自动实现事务操作,其实不然。存储过程只是提供的事务操作的支持。要实现事务操作,还得自己实现。 

       基本上方法有两个:

  •       SET XACT_ABORT


指定当 Transact-SQL 语句产生运行时错误时,Microsoft® SQL Server™ 是否自动回滚当前事务。

语法
SET XACT_ABORT { ON | OFF }

注释
       当 SET XACT_ABORT 为 ON 时,如果 Transact-SQL 语句产生运行     时 错误,整个事务将终止并回滚。为 OFF 时,只回滚产生错误的 Transact-SQL 语句,而事务将继续进行处理。编译错误(如语法错误)不受 SET XACT_ABORT 的影响。

对于大多数 OLE DB 提供程序(包括 SQL Server),隐性或显式事务中的数据修改语句必须将 XACT_ABORT 设置为 ON。唯一不需要该选项的情况是提供程序支持嵌套事务时。有关更多信息,请参见分布式查询和分布式事务。

SET XACT_ABORT 的设置是在执行或运行时设置,而不是在分析时设置。

 

 

 例:

      

None.gif create   proc  testproc
None.gif
as
None.gif
SET  XACT_ABORT  on
None.gif
begin   tran
None.gif
insert   into  tableA (field1)  values  ( ' aa ' )
None.gif
insert   into  tableB (field1)  values  ( ' bb ' )
None.gif
commit   tran
None.gif
SET  XACT_ABORT  off
None.gif

 

  • begin tran

         /*要实现的操作*/
       commit tran
       if @@error>0
       rollback

 

     例:

    

None.gif    create   proc  testproc
None.gif      
as
None.gif
None.gif      
begin   tran
None.gif     
insert   into  tableA (field1)  values  ( ' aa ' )
None.gif     
insert   into  tableB (field1)  values  ( ' bb ' )
None.gif     
commit   tran
None.gif     
if   @@error > 0
None.gif      
rollback
None.gif
None.gif


另外,在.NET的ADO.NET数据库编程中,可以使用SqlTransaction实现事务操作。

   例:

   

ExpandedBlockStart.gif ContractedBlock.gif Public   Sub RunSqlTransaction() Sub RunSqlTransaction(myConnString As String)
InBlock.gif    
Dim myConnection As New SqlConnection(myConnString)
InBlock.gif    myConnection.Open()
InBlock.gif    
InBlock.gif    
Dim myCommand As SqlCommand = myConnection.CreateCommand()
InBlock.gif    
Dim myTrans As SqlTransaction
InBlock.gif    
InBlock.gif    
' Start a local transaction
InBlock.gif
    myTrans = myConnection.BeginTransaction()
InBlock.gif    
' Must assign both transaction object and connection
InBlock.gif
    ' to Command object for a pending local transaction
InBlock.gif
    myCommand.Connection = myConnection
InBlock.gif    myCommand.Transaction 
= myTrans
InBlock.gif    
InBlock.gif    
Try
InBlock.gif      myCommand.CommandText 
= "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
InBlock.gif
      myCommand.ExecuteNonQuery()
InBlock.gif      myCommand.CommandText 
= "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
InBlock.gif
      myCommand.ExecuteNonQuery()
InBlock.gif      myTrans.Commit()
InBlock.gif      Console.
WriteLine("Both records are written to database.")
InBlock.gif    
Catch e As Exception
InBlock.gif      
Try
InBlock.gif        myTrans.Rollback()
InBlock.gif      
Catch ex As SqlException
InBlock.gif        
If Not myTrans.Connection Is Nothing Then
InBlock.gif          Console.
WriteLine("An exception of type " & ex.GetType().ToString() & _
InBlock.gif                            
" was encountered while attempting to roll back the transaction.")
InBlock.gif        
End If
InBlock.gif      
End Try
InBlock.gif    
InBlock.gif      Console.
WriteLine("An exception of type " & e.GetType().ToString()   "was encountered while inserting the data.")
InBlock.gif      Console.
WriteLine("Neither record was written to database.")
InBlock.gif    
Finally
InBlock.gif      myConnection.Close()
InBlock.gif    
End Try
ExpandedBlockEnd.gif
End Sub
  ' RunSqlTransaction
None.gif



 

转载于:https://www.cnblogs.com/weisai/archive/2005/07/07/188000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值