This article will introduce the key explanation of how to update or add a record in MsSqlServer and get the value of its identification column immediately after it is updated or added. This value is automatically numbered in ACCESS.
The ID value is OK, I won’t talk much, and I won’t write more codes, I will only write more key words. I believe that all the experts will understand it at a glance, haha. The method obtained in ACCESS is also attached later, although in ACCESS
Although there are many articles that obtain automatic numbering on the Internet, please put them here as a summary.
SettestRs=Server.CreateObject("ADODB.RecordSet")'Create the object to be tested
testRs.Open"[TestTable]",Conn,1,2' Assuming that Conn has been created and the database has been connected
testRs.AddNew
testRs("ColName1")="ColName1"
testRs("ColName2")="ColName2"
......
testRs("ColNameN")="ColNameN"
testRs.Update'Call the Update method and write the data in memory into the database immediately. The following sentence is the key
testRs.MoveLast' will move the last one
ID=testRs("ID")' This way you can get the automatic number of the new record just now
Don't forget to close the object at the end
testRs.Close:SettestRs=Nothing
Haha, I have tested the above methods in Windows 2000 ServerSp4+MsSQLServer+Asp/VB/DELPHI, etc., and they are all feasible. Because this article is for people with some basic reading, the above code cannot be run directly. If you have any questions about the above code, please contact me QQ: 115269, or go to my website to entertain and entertain: http://www.772.cn/
The following is a reposted How to insert a record in the Access database and get the automatically numbered ID value immediately
First of all, you must ensure that the way you obtain the record set supports bookmark attributes, such as 1 and 3
After inserting a record with an automatic numbering field, get the bookmark attribute value of the record
temp=rs.bookmark
Then
rs.bookmark=temp
Try it! !
Response.writers("ID").
By the way, I will explain what attribute BookMark is, because there are articles online now that the explanation of this is a bit ambiguous:)
Returns the bookmark that uniquely identifies the current record in the Recordset object (we can return its automatic number through this property after adding a new record in ACCESS), or sets the current record of the Recordset object to the record identified by a valid bookmark.
Set and return values
Sets or returns a variant expression that calculates a valid bookmark.
illustrate
Use the Bookmark attribute to save the current record's location and return to the record at any time. Bookmarks can only be used in Recordset objects that support bookmark functionality.
When opening a Recordset object, each record has a unique bookmark. To save the bookmark of the current record, assign the value of the Bookmark property to a variable. To quickly return to the record after moving to another record, set the Bookmark property of the Recordset object to the value of the variable.
Users may not be able to view the value of the bookmark, nor can they directly compare the bookmarks (the values of the two bookmarks pointing to the same record may be different).