欢迎光临
我们一直在努力

docmd.runsql 语句执行的操作查询如何回滚?-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

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

 

问题:

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

另外,请参考本站的文章:

关于事务处理
http://access911.net/index.asp?u1=a&u2=73fabf1e14dc

什么是事务处理?怎么进行事务处理?
http://access911.net/index.asp?u1=a&u2=71fabe1e13dc

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » docmd.runsql 语句执行的操作查询如何回滚?-数据库专栏,SQL Server
分享到: 更多 (0)