当前位置:Gxlcms > mysql > docmd.runsql语句执行的操作查询如何回滚?

docmd.runsql语句执行的操作查询如何回滚?

时间:2021-07-01 10:21:17 帮助过:28人阅读

问题: docmd.runsql 语句 执行 的 操作 查询 如何 回滚? 回答: 希望通过Docmd.RUNSQL实现事务的回滚(RollBack) 操作 很遗憾的说,Access无法法通过Docmd.RUNSQL来实现事务的回觥4蠹乙残碜⒁獾皆谀ocmd.runsql的 语句 操作 的帮助中,有一个选项是UseTrans

问题:

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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnacc2k/html/acadvsql.asp

人气教程排行