• 0

[C#/MySQL] Getting the last insert id after commit?


Question

I need to grab the last inserted id of a row I'm inserting into a dataset (using the MySQL C#/Connector). I update the database with a mysqldataadapter but I can't find a way to grab the last insert id once the row is commited to the database.

I looked into the debugger and it shows the primary key of the row inserted as {} (in other words null). I don't really want to have to call yet another mysql command to grab it as it will slow everything down I would think. What are my options?

5 answers to this question

Recommended Posts

  • 0

I'll admit I don't know much about MySQL, but from what I can find, you need to use the LAST_INSERT_ID() function.

For example:

INSERT INTO my_table (field1, field2, field3)
VALUES (@value1, @value2, @value3);
SELECT LAST_INSERT_ID();

You can find an example here on stackoverflow.com

  • 0

I read the stack overflow link. The only problem is I'm not actually using MySqlCommand (INSERT...). I'm just simply using a dataset, which I in turn use the NewRow() function and add my fields.

After this I call the mysqldataadapter.update(Dataset, string Table) function. It commits the changes to the mysql database but I can't find a way of getting that last insert id without calling a MySqlCommand object to retrieve it.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.