vb.net事务处理

[Visual Basic]
Public Sub RunSqlCeTransaction(myConnString As String)
   Dim myConnection As New SqlCeConnection(myConnString)
   myConnection.Open()
  
   Dim myCommand As New SqlCeCommand()
   Dim myTrans As SqlCeTransaction
  
   ' Start a local transaction
   myTrans = myConnection.BeginTransaction()
  
   ' Must assign both transaction object and connection
   ' to Command object for a pending local transaction
   myCommand.Connection = myConnection
   myCommand.Transaction = myTrans
  
   Try
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
      myCommand.ExecuteNonQuery()
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
      myCommand.ExecuteNonQuery()
      myTrans.Commit()
   Catch
      Try
         myTrans.Rollback()
      Catch e As SqlCeException
      ' Handle possible exception here
      End Try
   Finally
      myConnection.Close()
   End Try
End Sub

posted on 2006-09-12 13:14  奇远  阅读(801)  评论(0编辑  收藏  举报

导航