Search code examples
c#asp.net-mvcoracle-databasedapper

How do I get the last id entered with Dapper?


I'm querying DB Oracle and would like to return the entered id. I use a sequence that automatically generates the next id for me. I've already tried this, but I have an "ORA-00933 sql command terminated incorrectly" exception. What is the error? Thank you

public async Task<int> InsertSESSIONUSER_TAsync(SESSIONUSER_T obj)
    {
        string sql = "INSERT INTO SESSIONUSER_T (ID, USERNAME,PASSWORD,LOCALE,TIMEZONEID,EMAIL,CREATIONDATE, EMAILPEO) VALUES (USER_SEQUENCE.NEXTVAL,'TEMP',:PASSWORD,:LOCALE,:TIMEZONEID,:EMAIL,:CREATIONDATE,:EMAILPEO); SELECT CAST(SCOPE_IDENTITY() as int)";
        using (OracleConnection cnn = DBCConnectionFactory.Getconnection())
        {
            try
            {
                cnn.Open();
                int row = await cnn.ExecuteAsync(sql, obj);
                var result = await cnn.QueryAsync<int>(sql, obj);
                return result.Single();

            }
            catch (Exception ex)
            {
                ApplicationLogger.Logger.Error(ex, "InsertSESSIONUSER_TAsync");

            }
            finally
            {
                if (cnn?.State == System.Data.ConnectionState.Open)
                {
                    cnn.Close();
                }

            }
            return -1;
        }
    }

Solution

  • Post SOLVED! I used the transactions and everything worked! ;)