MySql Last Insert ID, Connector .net

后端 未结 6 2099
广开言路
广开言路 2020-12-01 16:22

I\'m using the MySql Connector .net, and I need to get the insert id generated by the last query. Now, I assume the return value of MySqlHelper.ExecuteNonQuery

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 17:02

    Try to use this query to get last inserted id -

    SELECT LAST_INSERT_ID();
    

    Then, run DbCommand.ExecuteReader method to get IDataReader -

    command.CommandText = "SELECT LAST_INSERT_ID()";
    IDataReader reader = command.ExecuteReader();
    

    ...and get information from the reader -

    if (reader != null && reader.Read())
      long id = reader.GetInt64(0);
    

    ...do not forget to close the reader;-)

提交回复
热议问题