Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
1)CRUD操作:
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Update(product); //session.Save(product);session.Delete(product); 或者_session.SaveOrUpdate(c);
transaction.Commit();
}
在NHibernate中提供了三种查询方式给我们选择:NHibernate查询语言(HQL,NHibernate Query Language)、条件查询(Criteria API,Criteria Query)、(根据示例查询(QBE,Query By Example)是条件查询的一种特殊情况)、原生SQL(Literal SQL,T-SQL、PL/SQL)。
2)简单查询–条件查询Criteria Query
- 据关键字查询:session.Get<Product>(productId);
- 据其它属性查询
- 返回一个值:session.CreateCriteria(typeof(Product)).Add(Restrictions.Eq(“Name”, name)).UniqueResult<Product>();
- 返回一个集合:session.CreateCriteria(typeof(Product)).Add(Restrictions.Eq(“Name”, name)).List<Product>();
- 除了用Eq外,还可用like,between等
3)SQL语句–HQL查询
- From子句: return _session.CreateQuery(“from Customer”) .List<Customer>();
-
select 子句 :
-
return _session.CreateQuery("select c.Firstname, count(c.Firstname) from Customer c group by c.Firstname").List<object[]>();
- where子句:return _session.CreateQuery(“from Customer c where c.Firstname=’YJing'”) .List<Customer>();
注:显示的名字为实体名而不是表名 -
order by子句:return _session.CreateQuery("from Customer c order by c.Firstname asc,c.Lastname desc").List<Customer>();
- group by子句:
return _session.CreateQuery("select c.Firstname, count(c.Firstname) from Customer c group by c.Firstname").List<object[]>();
- 参数传递:
return _session.CreateQuery("from Customer c where c.Firstname=:fn").SetString("fn", firstname).List<Customer>();