gpt4 book ai didi

vbscript - "Identity cannot be determined for newly inserted rows"在 ADO RecordSet AddNew 和 Update 之后

转载 作者:行者123 更新时间:2023-12-03 22:28:04 25 4
gpt4 key购买 nike

在 .vbs 文件中调用 AddNew 和 Update 后尝试编辑 ADO RecordSet 记录的字段时,我收到错误消息“无法确定新插入的行的标识”。但是,我能够访问从数据库返回的主键。

我不是在寻找解决方法(例如关闭记录集并通过其 ID 检索记录),我只是真的很想了解为什么会发生此错误。我已经在测试文件中简化了我的代码以排除任何其他问题。您在下面看到的是我正在执行的确切代码,不包含任何文件(我已经从连接字符串中删除了凭据)。

Dim connString : connString = "Provider=SQLOLEDB.1;Persist Security Info=True;Data Source=localhost;Initial Catalog=;User Id=;Password="

Dim conn, rsTaskLog, sSQL

Set conn = CreateObject("ADODB.Connection")
conn.Open connString

' Create a new task log entry.
Set rsTaskLog = CreateObject("ADODB.Recordset")
sSQL = "SELECT * FROM Test"
rsTaskLog.Open sSQL, conn, 1, 3, 1 'adOpenKeyset, adLockOptimistic, adCmdText
rsTaskLog.AddNew
rsTaskLog.Update

' Set the task log result.
rsTaskLog.Fields("test_int").Value = 1 ' Error occurs on this line.
rsTaskLog.Update

rsTaskLog.Close
Set rsTaskLog = Nothing

更新:

我能够通过在第一次更新后添加以下行来使此代码正常工作:

rsTaskLog.AbsolutePosition = rsTaskLog.AbsolutePosition

关于移动当前记录的一些事情是将 RecordSet 放回到可以编辑的状态(MoveLast 和 MoveFirst 也有效)。任何人都知道导致这种情况的幕后发生了什么?

最佳答案

我想出的解决方案是在 RecordSet 上调用第一个 Update 后立即添加以下代码行:

rsTaskLog.AbsolutePosition = rsTaskLog.AbsolutePosition

出于某种原因,移动光标位置会使 RecordSet 回到可以再次调用 Update 而不会产生错误的状态(MoveFirst 和 MoveLast 也有效,但通过将 AbsolutePosition 设置为自身,我们能够保持当前位置).我不完全确定这里的幕后发生了什么,如果您知道,请随时在评论中详细说明。

关于vbscript - "Identity cannot be determined for newly inserted rows"在 ADO RecordSet AddNew 和 Update 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31061584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com