使用DAO从SQL表中检索autonum / IDENTIFIER值。 [英] Retrieving autonum / IDENTIFIER value from SQL table using DAO.

查看:68
本文介绍了使用DAO从SQL表中检索autonum / IDENTIFIER值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在将Access后端转换为SQL Server Express。

前端程序(转换为Access 2003) )始终使用DAO。在

Access中,当我使用recordset.AddNew时,我可以检索

新记录的autonum值。这不会发生在SQL Server上,这当然会导致

错误(或至少在此代码中它会发生,因为有一个未处理的NULL

值)。当我从SQL服务器添加新记录

时是否有任何方法可以检索此值?或者我必须在VB中以编程方式执行此操作吗?


任何方向会很棒。


谢谢!

Hello,

I am in the midst of converting an Access back end to SQL Server Express.
The front end program (converted to Access 2003) uses DAO throughout. In
Access, when I use recordset.AddNew I can retrieve the autonum value for the
new record. This doesn''t occur with SQL Server, which of course causes an
error (or at least in this code it does since there''s an unhandled NULL
value). Is there any way to retrieve this value when I add a new record
from SQL server or will I have to do it programmatically in VB?

Any direction would be great.

Thanks!

推荐答案

试试:


选择

scope_identity()

-

Tom


-------------------------------------------------- -

Thomas A. Moreau,理学士,博士,MCSE,MCDBA

SQL Server MVP

加拿大多伦多

..

" Rico" < r c o l l e s s @ h e m m n n w w y y。完成本部分

CAPS>在消息新闻中写道:1fB_f.527
Try:

select
scope_identity()

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
"Rico" <r c o l l e n s @ h e m m i n g w a y . c o mREMOVE THIS PART IN
CAPS> wrote in message news:1fB_f.527


7a.323@pd7tw1no ...

你好,


我我正在将Access后端转换为SQL Server Express。

前端程序(转换为Access 2003)始终使用DAO。在

Access中,当我使用recordset.AddNew时,我可以检索

新记录的autonum值。这不会发生在SQL Server上,这当然会导致

错误(或至少在此代码中它会发生,因为有一个未处理的NULL

值)。当我从SQL服务器添加新记录

时是否有任何方法可以检索此值?或者我必须在VB中以编程方式执行此操作吗?


任何方向会很棒。


谢谢!
7a.323@pd7tw1no...
Hello,

I am in the midst of converting an Access back end to SQL Server Express.
The front end program (converted to Access 2003) uses DAO throughout. In
Access, when I use recordset.AddNew I can retrieve the autonum value for the
new record. This doesn''t occur with SQL Server, which of course causes an
error (or at least in this code it does since there''s an unhandled NULL
value). Is there any way to retrieve this value when I add a new record
from SQL server or will I have to do it programmatically in VB?

Any direction would be great.

Thanks!


Rico(rcollens @ hemmingway。共同修改这个部分)

写道:
Rico (r c o l l e n s @ h e m m i n g w a y . c o mREMOVE THIS PART IN CAPS)
writes:
我正在将Access后端转换为SQL Server
Express。前端程序(转换为Access 2003)始终使用DAO
。在Access中,当我使用recordset.AddNew时,我可以检索新记录的
autonum值。这不会发生在SQL Server上,
当然会导致错误(或者至少在这段代码中它会发生,因为它有一个未处理的NULL值)。有什么办法可以在我从SQL服务器添加新记录时检索这个
值,还是我必须在VB中以编程方式执行它?
I am in the midst of converting an Access back end to SQL Server
Express. The front end program (converted to Access 2003) uses DAO
throughout. In Access, when I use recordset.AddNew I can retrieve the
autonum value for the new record. This doesn''t occur with SQL Server,
which of course causes an error (or at least in this code it does since
there''s an unhandled NULL value). Is there any way to retrieve this
value when I add a new record from SQL server or will I have to do it
programmatically in VB?




最好使用存储过程来添加数据,而不是依靠

ADO生成代码。 Jet提供商很容易为您填写Autonumber,因为所有操作都在您的

流程空间中。但是由于SQL Server位于网络的另一端,

还有一个额外的往返来获取价值。


此外,使用SQL Server,make确保所有游标都是客户端。


一个示例存储过程:


创建过程insert_tbl @a int,

@b datetime,

@c varchar(23),

@id int AS

INSERT tbl(a,b, c)

VALUES(@ a,@ b,@ c)

SELECT @id = scope_identity


-

Erland Sommarskog,SQL Server MVP, es****@sommarskog.se


SQL Server 2005联机丛书
http://www.microsoft.com/technet/pro...ads/books.mspx

SQL Server 2000联机丛书在
http://www.microsoft.com/sql/prodinf...ons/books.mspx



It''s better to use stored procedures to add data, rather than relying on
ADO generating code behind your back. It''s easy for the Jet provider
to populate the Autonumber for you, because all operations are in your
process space. But since SQL Server is on the other end of the wire,
there is an extra roundtrip to get the value.

Also, with SQL Server, make sure that all your cursors are client-side.

A sample stored procedure:

CREATE PROCEDURE insert_tbl @a int,
@b datetime,
@c varchar(23),
@id int AS
INSERT tbl (a, b, c)
VALUES (@a, @b, @c)
SELECT @id = scope_identity


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


这篇关于使用DAO从SQL表中检索autonum / IDENTIFIER值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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