asp.net mvc - Get LAST_INSERT_ID from transaction -
i have following code:
using (mysqlconnection connection = new mysqlconnection(this.connectionstring)) { connection.open(); using (mysqltransaction transaction = connection.begintransaction()) { var r = connection.execute(@"insert tablea (`listid`, `parentlistid`, `name`, `userid`) values (null, @parentlistid, @name, @userid); insert `tablea_misc` (`listid`, `userid`, `permissions`) values (last_insert_id(), @ownerid, 'read'); select cast(last_insert_id() unsigned integer);", new { parentlistid = _parentlistid, name = _name, userid = _currentuser.user_id }, transaction); transaction.commit(); } }
how can retrieve the last_insert_id()
since r
returns number of rows inserted?
thank you
changing fixed:
using (mysqlconnection connection = new mysqlconnection(this.connectionstring)) { connection.open(); using (mysqltransaction transaction = connection.begintransaction()) { var lastid = connection.query<int>(@"insert tablea (`listid`, `parentlistid`, `name`, `userid`) values (null, @parentlistid, @name, @userid); insert `tablea_misc` (`listid`, `userid`, `permissions`) values (last_insert_id(), @ownerid, 'read'); select cast(last_insert_id() unsigned integer);", new { parentlistid = _parentlistid, name = _name, userid = _currentuser.user_id }, transaction); transaction.commit(); } }
Comments
Post a Comment