Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get the ID of the last added record?

Status
Not open for further replies.

angor

Programmer
Sep 25, 2005
12
UA
Hello!
I need to know the ID of the last added record into the DB (it is auto_increment).
is there some function, like last_id or smth. like that?
would be thankful.
Andrew
 
OK, but what if at one unpleasant moment it won't be the MAX? i think it can be possible . who knows how auto_increment works ?..
Andrew
 
Have you considered LastModified?
Microsoft said:
!LastName = "Warren"
.Update
' Move current record pointer to the most recently
' changed or added record.
.Bookmark = .LastModified
 
Dear Ken Reay!
I have tried this (i found SELECT @@IDENTITY), BUT it always returns 0. maybe i have to change some property of this auto_increment field? i 've tried to find it out, but didn't manage to.
i do need it very much, and would be thankful to everybody who could help me.

Best regards
Andrew
 
I am working on ADO.NET 2003. So I have such kind of a code:


private void Updating()
{
this.OleDbConnection1.Open();
string strsql = "UPDATE table1 SET name = 'Johnson'";
Oledbcommand cmd = new Oledbcommand(strsql,this.Oledbconnection1);
Oledbdatareader rdr1 = cmd.ExecuteReader();
rdr1.Open();
rdr1.Close();

string strsql1 = "SELECT @@IDENTITY";
Oledbcommand cmd1 = new Oledbcommand(strsql1,this.Oledbconnection1);
int last_num = cmd1.ExecuteScalar();

this.Oledbconnection1.Close();

and after that I have
last_num = 0
WHy???
Andrew
 
Hi

The SQL statement you give in your example is an UPDATE statement not an INSERT statement

I believe @@IDENTITY is only relevant after an INSERT statement

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top