NHibernate 有好几种数据库查询方式
1、原生SQL
var employeeQuery = Database.Session
.CreateSQLQuery("select * from Employee where FirstName = 'John'")
.AddEntity(typeof(Employee));
var employees = employeeQuery.List<Employee>(); 2、Hibernate Query Language(HQL)HN自己的一条语言,简称HQL var employeeQuery = Database.Session
.CreateQuery("select e from Employee as e"); var employees = Database.Session
.CreateQuery("select e from Employee as e ")
.List<Employee>();
//select e from Employee as e where e.Firstname = 'John'
var firstName = "John";
var employees = Database.Session
.CreateQuery("select e from Employee as e where e.Firstname = '"
+ firstName
+ "'")
.List<Employee>(); var employeeQuery = Database.Session
.CreateQuery("select e from Employee as e where e.Firstname = :firstName");
employeeQuery.SetParameter("firstName", "John");
var employees = employeeQuery.List<Employee>(); 3、Criteria API 标准API using (var transaction = database.Session.BeginTransaction())
{
var employeeQuery = database.Session.CreateCriteria<Employee>();
employeeQuery.Add(Restrictions.Eq("Firstname", "john"));
var employees = employeeQuery.List<Employee>();
transaction.Commit();
} using (var transaction = database.Session.BeginTransaction())
{
var employees = database.Session.CreateCriteria<Employee>()
.Add(Restrictions.Eq("Firstname", "john"))
.List<Employee>();
transaction.Commit();
} using (var transaction = database.Session.BeginTransaction())
{
var employees = database.Session.CreateCriteria<Employee>()
.Add(Restrictions.Between("DateOfJoining", DateTime.Now.AddYears(-1), DateTime.Now))
.List<Employee>();
transaction.Commit();
} 4、The QueryOver API
var employees = Database.Session.QueryOver<Employee>()
.Where(x => x.Firstname == "John")
.List<Employee>(); var employees = Database.Session
.QueryOver<Employee>()
.Where(x => x.DateOfJoining > DateTime.Now.AddYears(-1) &&
x.DateOfJoining < DateTime.Now)
.List<Employee>(); 5、LINQ
var employees = from e in Database.Session.Query<Employee>()
where e.Firstname == "John" select e;
var employees = Database.Session.Query<Employee>()
.Where(e => e.Firstname == "John"); var employees = from e in Database.Session.Query<Employee>()
where e.DateOfJoining > DateTime.Now.AddYears(-1) &&
e.DateOfJoining < DateTime.Now
select e;
var employees = Database.Session.Query<Employee>()
.Where(e => e.DateOfJoining > DateTime.Now.AddYears(-1) &&
e.DateOfJoining < DateTime.Now);

NHibernate 有好几种数据库查询方式的更多相关文章

  1. MySql、SqlServer、Oracle 三种数据库查询分页方式

    SQL Server关于分页 SQL 的资料许多,有的使用存储过程,有的使用游标.本人不喜欢使用游标,我觉得它耗资.效率低:使用存储过程是个不错的选择,因为存储过程是颠末预编译的,执行效率高,也更灵活 ...

  2. mybatis两种嵌套查询方式

    1,推荐用第一种 <select id="getTeacher2" resultMap="TeacherStudent"> select s.id ...

  3. drill 数据库查询方式简单说明

    1. mysql   select * from mysql-storage.mysqldb.mysqltable   2. oracle    select * from oracle-storag ...

  4. iBatis.Net(C#)数据库查询

    引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html  摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...

  5. IBatis.Net学习笔记五--常用的查询方式

    在项目开发过程中,查询占了很大的一个比重,一个框架的好坏也很多程度上取决于查询的灵活性和效率.在IBatis.Net中提供了方便的数据库查询方式. 在Dao代码部分主要有两种方式:1.查询结果为一个对 ...

  6. 转载 50种方法优化SQL Server数据库查询

    原文地址 http://www.cnblogs.com/zhycyq/articles/2636748.html 50种方法优化SQL Server数据库查询 查询速度慢的原因很多,常见如下几种: 1 ...

  7. Oracle 数据库(oracle Database)Select 多表关联查询方式

    Oracle数据库中Select语句语法及介绍 SELECT [ ALL | DISTINCT ] <字段表达式1[,<字段表达式2[,…] FROM <表名1>,<表名 ...

  8. Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加

    Hibernate的添加,修改,查询(三种查询方式)的方法: 案例演示: 1:第一步,导包,老生常谈了都是,省略: 2:第二步,创建数据库和数据表,表结构如下所示: 3:第三步创建实体类User.ja ...

  9. Hibernate的Api以及三种查询方式

    Hibernate  Api |-- Configuration       配置管理类对象 config.configure();    加载主配置文件的方法(hibernate.cfg.xml) ...

随机推荐

  1. Shell编程-12-Shell脚本规范及调试

    目录 Shell脚本规范 Shell脚本调试 Shell脚本规范     良好的代码规范不仅方便阅读,也利于维护和提升开发效率.因此建议大家在编写Shell脚本时养成良好的代码习惯.今天就和大家探讨一 ...

  2. linux 各项分布(个人记录)

    1.根目录文件 root:存放root用户的相关文件home:存放普通用户的相关文件bin :存放常用命令的目录sbin:要具有一定权限才可以使用的命令mnt :挂在光驱和软盘的目录boot:存放引导 ...

  3. JavaScript相关基础知识点

    JavaScript简介: JavaScript是脚本语言,是一种轻量级的编程语言,是可插入 HTML 页面的编程代码,插入 HTML 页面后,可由所有的现代浏览器执行. JavaScript使用: ...

  4. Leetcode--1. Two Sum(easy)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. POJ3181--Dollar Dayz(动态规划)

    Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. ...

  6. HDU 4893 线段树的 点更新 区间求和

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. MySQL表名大小写设置

    1 简介    在MySQL中,数据库对应数据目录中的目录.数据库中的每个表至少对应数据库目录中的一个文件(也可能是多个,取决于存储引擎).因此,所使用操作系统的大小写敏感性决定了数据库名和表名的大小 ...

  8. unigui验证微信服务器的有效性

    UNIGUI验证微信服务器的有效性: //////////////////////////////////////////// //UniGUIServerModuleHTTPCommand //公众 ...

  9. DELPHI获取宽带IP

    DELPHI获取宽带IP   DELPHI获取宽带IP procedure TForm1.Button1Click(Sender: TObject);varurl: string;beginurl : ...

  10. Azure DevOps Server: 使用Rest Api获取拉取请求Pull Request中的变更文件清单

    需求: Azure DevOps Server 的拉取请求模块,为开发团队提供了强大而且灵活的代码评审功能.拉取请求中变更文件清单,对质量管理人员,是一个宝贵的材料.质量保障人员可以从代码清单中分析不 ...