get autoNumber value from database

情到浓时终转凉″ 提交于 2019-12-02 00:10:56

see this:

http://msdn.microsoft.com/en-us/library/ks9f57t0(v=VS.80).aspx

See the section labeled "Retrieving Microsoft Access Autonumber Values"

sort version

OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY",
            connection)

I do not believe SCOPE_IDENTITY() exist in access

I'd suggest moving the query from inline sql to a stored procedure. Not so much to stop SQL injection but because you can set the stored procedure to return the value of the identity (autonumber) field its just created. simply have it return SCOPE_IDENTITY after running the sql.

all that is assuming you are using SQL server (I'm not familiar with other db's but would assume they have similar functionality)

see link for more info

http://msdn.microsoft.com/en-us/library/ks9f57t0.aspx

EDIT: WOW this was answered fast! apologies for duplicate answers

EDIT2: looks like access exposes a variable @@IDENTITY that contains the last used/created autonumber. so you should be able to run and inline query and follow it with another along the lines of 'SELECT @@IDENTITY AS prevID'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!