MS-Access 中的提交事务无法写入数据库

问题描述

我正在使用 ADO 记录集。
我围绕需要一起发生的一组特定查询实现了事务。
结果是什么都没有提交。
它直接遍历代码,点击提交,从不点击回滚,但没有任何插入或更新出现在数据库中。

经过审核,我似乎使用 ADO 进行更新,但使用 DAO 进行交易。
在进一步审查中,他们使用 CurrentProject.connection 作为查询的连接。看起来它也来自 DAO。

我想我希望事务与插入和更新在同一个连接上。

所以我需要从 ADODB 获取一个连接对象。感觉这应该可行,但失败了。

Private conn As ADODB.connection

' Set the connection if you want the same one each time
Public Sub SetConnection()
    If conn Is nothing Then
        Set conn = New ADODB.connection
    End If
End Sub

' Close the connection when finished
Public Sub CloseConnection()
    Set conn = nothing
End Sub

' Get the current connection or a fresh one
Public Function GetConnection() As connection
    If conn Is nothing Then
' This fails with "Object required' and looks like an empty string.
        Set GetConnection = New ADODB.connection
' This won't compile
'       Set GetConnection = ADODB.connection
' This won't compile with a Type Mismatch error.
'       Set GetConnection = New CurrentProject.connection 
' This looks like it works but nothing inside the transaction get committed.
'       Set GetConnection = CurrentProject.connection 
    Else
        Set GetConnection = conn
    End If
End Function

如果我完全删除 Begin/Commit 事务语句,那么所有内容都会正确写入数据库,当然除非出现错误

实现 ADO 事务的正确方法是什么?

交易代码不太有趣。

Public Sub Begin()
    If Not (conn Is nothing) Then
        conn.BeginTrans
    End If
End Sub
Public Sub Commit()
    If Not (conn Is nothing) Then
        conn.CommitTrans
    End If
End Sub
Public Sub Rollback()
    If Not (conn Is nothing) Then
        conn.RollbackTrans
    End If
End Sub

解决方法

无权进行测试,但试试这个:

Private conn As ADODB.connection


Public Sub CloseConnection()
    Set conn = Nothing
End Sub

'Make sure you specify return type as ADODB.Connection
Public Function GetConnection() As ADODB.Connection
    If conn Is Nothing Then Set conn = CurrentProject.Connection
    Set GetConnection = conn
End Function
,

我不知道为什么使用来自 CurrentProject.Connection 的连接时使用 BeginTrans 不起作用,但它不起作用。

这是最终的解决方案。
在需要连接的任何地方使用 GetConnection()。例外情况是您不希望在出现故障时回滚的项目,例如日志记录。
像往常一样调用开始、提交和回滚。 Begin 将创建其他人将使用的持久连接。提交后,它将返回使用项目连接。或者,您可以为每个查询创建一个新的 ADO 连接。

Private conn As ADODB.connection

'Make sure you specify return type as ADODB.Connection or it doesn't work
Public Function GetConnection() As ADODB.connection
    If conn Is Nothing Then
'        Set GetConnection = New ADODB.connection
        Set GetConnection = CurrentProject.connection
    Else
        Set GetConnection = conn
    End If
End Function

Public Sub SetConnection()
On Error GoTo ErrHandler:
    If conn Is Nothing Then
'        Set conn = CurrentProject.connection
        Set conn = New ADODB.connection
        conn.Open GetConnectionString
    End If
ErrHandler:
    Set conn = Nothing
End Sub

Public Sub CloseConnection()
    Set conn = Nothing
End Sub

Public Sub Begin()
    SetConnection
    If Not (conn Is Nothing) Then
        conn.BeginTrans
    End If
End Sub

Public Sub Commit()
    If Not (conn Is Nothing) Then
        conn.CommitTrans
        CloseConnection
    End If
End Sub

Public Sub Rollback()
    If Not (conn Is Nothing) Then
        conn.RollbackTrans
        CloseConnection
    End If
End Sub

' Can be any table,just need to get the conn string
Public Function GetConnectionString() As String
    GetConnectionString = CurrentDb.TableDefs("System Log").Connect
End Function