NHibernate There are several ways to query the database

Source: Internet
Author: User

NHibernate There are several ways to query the database 1, native Sqlvar employeequery = Database.Session.CreateSQLQuery ("select * from Employee where FirstName = ' John '). Addentity (typeof (Employee)), var employees = employeequery.list<employee> (); 2, Hibernate Query Language (HQL) HN own language, referred to as Hqlvar employeequery = Database.Session.CreateQuery ("Select E from Employee as E"); var employees = Database.s Ession. CreateQuery ("Select E from Employee as E"). List<employee> ();//select e from Employee as e where e.firstname = ' john ' var Firstname = ' John '; var employees = Data Base. 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 Standard apiusing (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 apivar 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, Linqvar employees =From E in database.session.query<employee> () where e.firstname = = "John" Select E;var employees = database.se Ssion. Query<employee> (). WHERE (E = = E.firstname = = "John"); var employees = from E in database.session.query<employee> () where e.dateofjoin ing > DateTime.Now.AddYears ( -1) &&e.dateofjoining < datetime.nowselect E;var employees = Database.session.query<employee> (). Where (e = e.dateofjoining > DateTime.Now.AddYears ( -1) &&e.dateofjoining < DateTime.Now);

NHibernate There are several ways to query the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.