views:

725

answers:

2

Hi, How could I get the current DateTime from the database server, using NHibernate? I tried to use the current_date expression, but it doesn't seem to work outside the where clause. What I need is something like SELECT getdate(). This query in SQL server gets de current datetime from the server, I just don't know how to put it in HQL dialect.

Thanks

+2  A: 

One option is just to use a named SQL query in your mapping file:

<sql-query name="CurrentDate">
  <![CDATA[
    select getdate()
  ]]>
</sql-query>

Then in your calling code:

IQuery q = session.GetNamedQuery("CurrentDate");
var date = q.UniqueResult<DateTime>();

Of course, this only works if you're looking to retrieve only the date and not the date plus other parts of your entities.

Sean Carpenter
Thanks Sean. This would only work in SQL Server, right?
DiegoCofre
Right - this syntax would only work in SQL Server (and maybe Sybase). This isn't a great solution if database portability is important.
Sean Carpenter
A: 

Hi,

This is very good example. Can we have multiple queries in one mapping file. For example,

Also is there any way to mention the parameters to the query? For eg. Select * from Orders where OrderID = xx. The value of xx should be passed as a value to the where clause of the query.

Thanks in advance, Arun.K.S [email protected] [email protected]