Chinaunix首页 | 论坛 | 博客
  • 博客访问: 669769
  • 博文数量: 759
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 4845
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 13:51
文章分类

全部博文(759)

文章存档

2011年(1)

2008年(758)

我的朋友

分类:

2008-10-27 13:57:22


  问题:
  
  docmd.runsql 语句执行的操作查询如何回滚?
  
  回答:
  
  希望通过Docmd.RUNSQL实现事务的回滚(RollBack)操作
  
  很遗憾的说,Access无法法通过Docmd.RUNSQL来实现事务的回觥4蠹乙残碜⒁獾皆贒ocmd.runsql的语句操作的帮助中,有一个选项是UseTransaction。这个选项的是用来确认是否对该语句进行事务性的操作。如果选择True(默认为True),那么所有的操作都将被当作是一个单独的原子操作来对数据库进行操作;如果选择是False,那么操作将不会被当作事务(在多用户的情况下可能会出现Dirty Read)的情况。但是这些事务都是在内部完成的,我们无法显示的通过申明commit或者rollback来控制其操作。
  
  根据我的经验,ACCESS也无法通过Docmd.OPENQUERY来完成类似的事务显示操作。如果大家希望实现事务的操作,唯一的用法就是通过WorkSpaceObject.BeginTrans来实现。在Access VBA的帮助文件中,大家可以找到如下的示例:
  'BeginBeginTransVB
  
    'To integrate this code
    'replace the data source and initial catalog values
    'in the connection string
    
  Public Sub Main()
    On Error GoTo ErrorHandler
  
    'recordset and connection variables
    Dim Cnxn As ADODB.Connection
    Dim strCnxn As String
    Dim rstTitles As ADODB.Recordset
    Dim strSQLTitles As String
    'record variables
    Dim strTitle As String
    Dim strMessage As String
    
    ' Open connection
    strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _
      "Initial Catalog='Pubs';Integrated Security='SSPI';"
    Set Cnxn = New ADODB.Connection
    Cnxn.Open strCnxn
    
    ' Open recordset dynamic to allow for changes
    Set rstTitles = New ADODB.Recordset
    strSQLTitles = "Titles"
    rstTitles.Open strSQLTitles, Cnxn, adOpenDynamic, adLockPessimistic, adCmdTable
    
    Cnxn.BeginTrans
    
    ' Loop through recordset and prompt user
    ' to change the type for a specified title
    
    rstTitles.MoveFirst
    
    Do Until rstTitles.EOF
      If Trim(rstTitles!Type) = "psychology" Then
        strTitle = rstTitles!Title
        strMessage = "Title: " & strTitle & vbCr & _
        "Change type to self help?"
  
        ' If yes, change type for the specified title
        If MsgBox(strMessage, vbYesNo) = vbYes Then
          rstTitles!Type = "self_help"
          rstTitles.Update
        End If
      End If
    rstTitles.MoveNext
    Loop
  
    ' Prompt user to commit all changes made
    If MsgBox("Save all changes?", vbYesNo) = vbYes Then
      Cnxn.CommitTrans
    Else
      Cnxn.RollbackTrans
    End If
  
    ' Print recordset
    rstTitles.Requery
    rstTitles.MoveFirst
    Do While Not rstTitles.EOF
      Debug.Print rstTitles!Title & " - " & rstTitles!Type
      rstTitles.MoveNext
    Loop
  
    ' Restore original data as this is a demo
    rstTitles.MoveFirst
    
    Do Until rstTitles.EOF
      If Trim(rstTitles!Type) = "self_help" Then
        rstTitles!Type = "psychology"
        rstTitles.Update
      End If
      rstTitles.MoveNext
    Loop
   
    ' clean up
    rstTitles.Close
    Cnxn.Close
    Set rstTitles = Nothing
    Set Cnxn = Nothing
    Exit Sub
    
  ErrorHandler:
    ' clean up
    If Not rstTitles Is Nothing Then
      If rstTitles.State = adStateOpen Then rstTitles.Close
    End If
    Set rstTitles = Nothing
    
    If Not Cnxn Is Nothing Then
      If Cnxn.State = adStateOpen Then Cnxn.Close
    End If
    Set Cnxn = Nothing
    
    If Err <> 0 Then
      MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
  End Sub
  
  
  'EndBeginTransVB
  
  最后,强烈推荐大家阅读下列文档,在文档有一个章节:Transactions在Access中的用法和定义
  Advanced Microsoft Jet SQL for Access 2000
  
【责编:admin】
--------------------next---------------------

阅读(239) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~