In a project of mine, I had to get the last inserted ID from the database so that I can sync newly inserted data with in memory data without doing a SELECT * FROM my_db;
query i.e. I am doing a SELECT * FROM my_db WHERE id=new_index
.
Now, I know that MySQL, which I'm using, supports the query select last_insert_id();
, so I figured I would just make that command and execute it. But upon digging into C# (the programming language that I'm using for the project) I've discovered that the MySqlCommand
has a LastInsertedId
field.
Now what puzzles me is that I expected the getter of the field to just do the same select last_insert_id();
query, but upon looking at the logs from MySQL, I see no such query being made at all.
So my question is, how does MySqlCommand.LastInsertedId
know what ID was assigned to the tuple that I'm inserting when the ID is auto generated by the MySQL server?