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

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
 
L

Leonid Raiz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top