Sander Declerck
Sander Declerck

Reputation: 2535

Get the value of an auto increment PK column

I've got the following table in MySql: Contact:

Now, I'm writing an INSERT INTO in my c# program, but the Id column is defined to auto increment, so my statement looks like this: INSERT INTO Contact (Name, FirstName) VALUES ("Sander", "Declerck")

So how can I get the new value for my object?

Upvotes: 1

Views: 1384

Answers (2)

Douglas
Douglas

Reputation: 37761

In MySQL, you can use SELECT LAST_INSERT_ID(), or your API wrapper might let you call mysql_insert_id().

Upvotes: 1

Quassnoi
Quassnoi

Reputation: 425391

Issue this query:

SELECT  LAST_INSERT_ID()

right after the INSERT.

Upvotes: 2

Related Questions