A. Hamdi A. Hamdi - 1 year ago 111
MySQL Question

whats the right version for using either scope_identity() ,@@identity (),last_insert_id() in mysql

im working on asp.net c# ,i tried all 3 of them but none worked ,
this is the sql error(You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID()')
this is my query:

String st = ("INSERT INTO `service` VALUES ('','" + noms + "','" + Desc + "','3','" + ad + "','" + vil + "','" + pays + "')"+" SELECT SCOPE_IDENTITY()");

Answer Source

You can use LastInsertedId property of MySqlCommand like this :

// cmd is MySqlCommand objet
long lastInsertedId = cmd.LastInsertedId;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download