Conversion of values [closed]

0

I need the string "hql" which would be my select to convert to int so I can do the comparison of values in an if.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogWeb.ViewsModels;
using BlogWeb.DAO;
using BlogWeb.Models;
using BlogWeb.Filters;
using BlogWeb.Controllers;
using NHibernate;
using BlogWeb.Infra;

namespace BlogWeb.Controllers
    {
    public class GerenciamentoKM
        {
        private Rota p;
        private ISession session;
        public GerenciamentoKM(ISession session, Rota p, RotaDAO dao)
            {
            this.p = p;
            this.session = session;
            }
        public void soma(Rota post)
            {
            string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
            int km_t = Convert.ToInt16(hql);
            if (km_t <= p.Km_Atual)
                {
                    ITransaction tx = session.BeginTransaction();
                    session.Update(post);
                    tx.Commit();
                    }
            }
        }
    }

This does not work, I tried after running the query before

public void soma(Rota post)
        {
        string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
        IQuery query = session.CreateQuery(hql);
         query.List<Rota>();
        int km_t = Convert.ToInt16(hql);
        if (km_t <= p.Km_Atual)
            {
                ITransaction tx = session.BeginTransaction();
                session.Update(post);
                tx.Commit();
                }


        }

That way it did not work either, so I tried to start it on the outside and pull it to public.

public IList<Rota> Lista()
        {
        string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
        IQuery query = session.CreateQuery(hql);
        return query.List<Rota>();
        }
public void soma(Rota post)
        {
        string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
        if (Lista <= p.Km_Atual)
            {
                ITransaction tx = session.BeginTransaction();
                session.Update(post);
                tx.Commit();
                }


        }
    
asked by anonymous 07.06.2017 / 20:02

2 answers

2

Try this way:

string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
SqlCommand cmd = new SqlCommand(hql, conn); //conn é a string da conexão.
conn.Open();
km_t = (Int32)cmd.ExecuteScalar();
    
08.06.2017 / 00:51
0

Use the "top 1" parameter, so your SQL statement will return a single value and not a list of values:

        SELECT top 1 p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)  
    
07.06.2017 / 21:37