How to get a value of primary key for a newly inserted mdb record?

  • Thread starter Thread starter Leonid Raiz
  • Start date Start date
L

Leonid Raiz

I am a newbie using C# to develop an Access application.
Hopefully there is a simple answer to my question.
I use OleDbCommand ExecuteNonQuery to insert a new record into DB.
How do I get back the value that Access just assigned to an auto-increment
field?
Thanks for your help
 
Thanks for the pointer. The article gives an example of using data adapter and using an event handler. Based on provided information I tried a simpler approach of getting identity value directly (w/o data adapter or event handler) and everything seems to be working.

The code turned out to be really simple. I.E.

insertCommand.ExecuteNonQuery();
OleDbCommand identityCommand = new OleDbCommand("SELECT @@IDENTITY", connection);
int ID= (int) identityCommand .ExecuteScalar();

Thanks,
- LR
 
Back
Top