问题描述
如何使用NHibernate从数据库服务器获取当前的DateTime?
我试图使用current_date表达式,但在where子句外似乎不起作用。我需要的是像 SELECT getdate()
。 SQL Server中的这个查询从服务器获取当前的datetime,我只是不知道如何把它放在HQL方言中。
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.
谢谢
推荐答案
一个选项只是在映射文件中使用命名SQL查询:
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.
这篇关于NHibernate:检索当前的数据库服务器DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!