如何通过ADODB检索自动编号的字段 [英] How to retrieve autonumbered field via ADODB

查看:58
本文介绍了如何通过ADODB检索自动编号的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:MS Access和JScript

Context: MS Access and JScript

我有一张桌子,我在其中插入一个字段的数据,以便自动编号触发并给我该记录的唯一ID,即

I have a table into which I insert one field's worth of data so that the autonumber will fire and give me that record's unique id, viz

oConn.Execute( "INSERT INTO tbl (DateTimeStamp) VALUES (" + newNow + ");");

在这种情况下,newNow是一个Double值,它会自动强制转换为Date.

in this case, newNow is a Double value which is automagically coerced into a Date.

完成后,我想检索应该在INSERT点填充的自动编号ID字段.到目前为止,我一直在使用ADODB.Recordset进行此操作,但一直在尝试使用ADODB.Connection的Execute(),希望它可能会更快.

Once that's done, I would like to retrieve the autonumbered ID field which should have been filled at the point of INSERT. Up until now I've been using ADODB.Recordset for this stuff, but have been experimenting with ADODB.Connection's Execute() in the hope that it might be faster.

推荐答案

使用oConn作为ADODB.Connection,在VBA中,可以很方便地检索从该连接插入的最后一个自动编号值.看看是否可以转换为JScript.

With oConn as an ADODB.Connection, in VBA the following is convenient to retrieve the last autonumber value inserted from that connection. See if that translates to JScript.

oConn.Execute("SELECT @@Identity")(0)

您可以要求db引擎明确地执行此操作,而不是将双精度值自动转换为日期/时间.

Rather than automagically casting the double value to Date/Time, you can ask the db engine to do it explicitly.

"INSERT INTO tbl (DateTimeStamp) VALUES (CDate(" + newNow + "));"

这篇关于如何通过ADODB检索自动编号的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆