2010-11-05 114 views
1

如何使用NHibernate编写HQL查询。我必须包括哪些命名空间,以便一切正常。其实我有两张表格Ticket和Trip,我希望在Trip中没有Ticket中相应条目的记录。门票中有一个tid字段,提供Trip ID。任何人都可以从开始解释我将如何编写NHibernate HQL查询?NHibernate HQL查询

+0

你试过在谷歌搜索这个吗? – 2010-11-05 17:52:41

+0

...或阅读文档... – 2010-11-05 19:04:02

回答

3

您不需要任何特殊的名称空间来使用HQL。只需创建一个简单的NHibernate项目,即可开始编写HQL。

下面是新的NHibernate 3.0 Cookbook的一个示例,您还应该检查Nhibernate in Action书籍,其中有更详细的HQL示例。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using NHibernate.Cfg; 
using NHibernate; 

namespace ExecutableHQL 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     log4net.Config.XmlConfigurator.Configure(); 
     var nhConfig = new Configuration().Configure(); 
     var sessionFactory = nhConfig.BuildSessionFactory(); 

     using (var session = sessionFactory.OpenSession()) 
     { 
     using (var tx = session.BeginTransaction()) 
     { 
      int count = (int) session.CreateQuery("select count(*) from Trip").UniqueResult(); 

      tx.Commit(); 
     } 
     } 
    } 
    } 
} 
0
[HttpGet] 
    public int GetCount() 
    { 
     var myQuery = session.CreateQuery(@" 
     select COUNT(*) from Table as t where 
     t.Id = :Id"); 
     myQuery.SetParameter("Id", this.Id); 
     int count = Convert.ToInt32(myQuery.UniqueResult()); 
     return count; 
    }