I need to convert a string to int inside a hql

0

I need to change the string to int, but it is located inside a hql, I will probably have to terminate the query, but it from the error to enter the database, my code is a c # with asp.net, mvc, nhibernate.

public void soma(Rota post)
        {
        //* Tentativa de criação de query ja foi efetuada e não deu certo.
        string hql = "SELECT Km_Atual FROM Rota WHERE Id=LAST_INSERT_ID()";
        //* Tentar converter o hql para int, para fazer comparação em if.
        int Km_Ultima = Convert.ToInt16(hql); 
        if (Km_Ultima <= p.Km_Atual)
    
asked by anonymous 13.06.2017 / 18:00

1 answer

0

As LINQ said, you are trying to convert the string "SELECT Km_Atual FROM Rota WHERE Id=LAST_INSERT_ID()" instead of the value that the database will return.

To do this you will have to make a connection to the database and execute the command, so you convert the returned value and store it in a variable.

Example:

string hql = "SELECT Km_Atual FROM Rota WHERE Id=LAST_INSERT_ID()";
IQuery query = applicationSession.CreateQuery(hql).UniqueResult();
int Km_Ultima = int.Parse(query.ToString());

if (Km_Ultima <= p.Km_Atual)
    
14.06.2017 / 01:40