How do I retrieve the last insert id for a 'serial' field?
Posted: Fri 26 Nov 2004 07:28
Hi there...
I noticed using the PostgreSQLDirect.NET provider that PgSQLCommand doesn't contain a property "InsertID" which is present in the MySQLCommand equivalent. There is a property called "InsertOid" but that's not the same thing.
So, supposing I create a table "animal" with two columns:
... and then I use a PgSqlCommand called InsertCommand to add a new animal to the table (using the following code):
... is there some way to interrogate the InsertCommand object to find out the value of the most recently inserted value for 'id' ?
Thanks in advance for your help...
Cheers,
DaveW.
I noticed using the PostgreSQLDirect.NET provider that PgSQLCommand doesn't contain a property "InsertID" which is present in the MySQLCommand equivalent. There is a property called "InsertOid" but that's not the same thing.
So, supposing I create a table "animal" with two columns:
Code: Select all
CREATE TABLE animal (
id serial NOT NULL,
description varchar(80)
);
Code: Select all
///
/// this method should insert a new chicken into the database and
/// return the new chicken's auto-incremented id value.
///
private int insertAnotherChicken() {
// the command to be used for chicken insertion
PgSqlCommand InsertCommand = new PgSqlCommand();
...
InsertCommand.CommandText = "INSERT INTO animal(description) VALUES ('Chicken')";
...
InsertCommand.ExecuteNonQuery();
// HELP!!!!
// how do I return the id of the newly inserted chicken
return ???;
}
Thanks in advance for your help...
Cheers,
DaveW.