Is there a way in ADO.NET to retrieve the auto-generated ID after executing an INSERT-statement? I know about SELECT @@IDENTITY for Sql Server and SELECT last_insert_id() for MySQL, but what I need is a database-independent way to retrieve the ID.
Is there a way in ADO.NET to retrieve the auto-generated ID after executing an
Share
I dont think a way exists, because the command to get last inserted id is vendor specific. What you can do is to incorporate the query to get last inserted id in your main query (which I understand is not what you want) and then write a C# function to read it irrespective of your db (without relying on specific ADO.NET type but on the methods specified on interface
IDbDataReaderorIDbCommand)Or else you will have to rely on some ORM solution like Entity Framework which will handle all this for you saving you from the hassle of manually getting last inserted id, though the implementation will be very db specific. You can see a nice discussion on that here:
getting identity row value using ADO.NET Entity
You can see this link which will give you some insight on directly accessing the records relying on dataadapters (but I dont like it).