我正在尝试将 Excel 中的数据插入链接到 Sharepoint 列表的 Access 表中。我得到了这个工作,除了我的交易不工作这一事实。新项目始终会立即显示在 Sharepoint 中的
rs.Update
行之后。而ws.Rollback
行什么也没做。谁能解释为什么会发生这种情况并且可以使其发挥作用吗?代码:
Private Sub InsertData()
Dim dbConnection As New ADODB.Connection
Dim dbCommand As New ADODB.Command
Dim ws As DAO.Workspace
Dim rs As DAO.Recordset2
Dim db As DAO.Database
Dim sowItem As clsSowingEntry
Dim crit As String
Dim dKey As Variant
Dim rsEvent As DAO.Recordset2
On Error GoTo errHandler
If datadict.Count = 0 Then
Set datadict = Nothing
MsgBox ("No valid sowing events were found.")
End
End If
Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase(accessPath)
Set rs = db.OpenRecordset("FormData")
Set rsEvent = db.OpenRecordset("EventType")
ws.BeginTrans
For Each dKey In datadict.Keys
Set sowItem = datadict(dKey)
crit = "EventTypeId = 1 AND ProductionLineCode = '" & sowItem.ProductionLine & "' AND ProductCode = '" & sowItem.ProductCode & "' AND EventDate = " & sowItem.SowingDate
rs.FindFirst crit
If rs.NoMatch Then
'Insert New Record
rs.AddNew
rs!SowingDate = sowItem.SowingDate
rs!EventDate = sowItem.SowingDate
rs!EmployeeName = sowItem.SowerName
rs!EventTypeId = sowItem.EventTypeId
rs!ProductionLineCode = sowItem.ProductionLine
rs!ProductCode = sowItem.ProductCode
rs!UnitCode = sowItem.UnitCode
rs!GreenHouseCode = sowItem.GreenHouseCode
rs!Quantity = sowItem.ActualCellCount
rs!SeedBatchNumber = sowItem.SeedBatchNo
rs.Update
End If
Next
If MsgBox("Save changes?", vbQuestion + vbYesNo) = vbYes Then
ws.CommitTrans
Else
ws.Rollback
End If
rs.Close
db.Close
ws.Close
Set db = Nothing
Set rs = Nothing
Set ws = Nothing
Set sowItem = Nothing
Set datadict = Nothing
Exit Sub
errHandler:
ws.Rollback
rs.Close
db.Close
ws.Close
Set db = Nothing
Set rs = Nothing
Set ws = Nothing
Set sowItem = Nothing
Set datadict = Nothing
Call Common.FatalError(Err.Description)
End Sub
当您将 Access 表链接到 SharePoint 列表时,您实际上是在创建实时连接。对链接表所做的更改会立即反映在 SharePoint 列表中,绕过任何本地事务管理。
1。导入/导出方式:
2。错误处理和重试逻辑: 在每次更改时在数据库代码中实现健壮且仔细的错误处理程序,以检测数据插入期间的问题,并重试或回滚这些更改。
为了完整起见,我阅读了其他技术,包括 API 的使用和第三方工具,但这两种解决方案是我最喜欢的,尤其是第二个。