01-04-01 "Nhibernate (version 3.3.1.4000) in and out of the lake" native SQL query

Source: Internet
Author: User

Nhibernate supports native SQL queries:

1         /// <summary>2         ///using native SQL queries3         /// </summary>4         /// <param name= "datetime" ></param>5         /// <returns></returns>6          PublicIlist<customer> Getcustomersbydatetimeusingsql (stringdatetime)7         {8Ilist<customer> resucustomers =NULL;9 TenISession session =_sessionmanager.getsession (); OneITransaction transaction =session. BeginTransaction (); A  -             ; -             Try the             { -                 //method One: Directly assemble SQL statements -                 //String sql = String.Format ("SELECT distinct customeralias.*" + -                 //"from Customer customeralias inner join [Order]" + +                 //"On customeralias.customerid = [Order]." CustomerId "+ -                 //"where [Order]." OrderDate >= ' {0} '; ", datetime); +                 ////When querying with native SQL statements, assemble entity classes with addentity A                 //resucustomers = session. Createsqlquery (SQL). Addentity ("Customeralias", typeof (Customer)). List<customer> (); at  -  -                 //mode two: Using Query parameters -String Sqlusingparameter = String.Format ("SELECT DISTINCT customeralias.*"+ -                                                          "From Customer customeralias inner join [Order]"+ -                                                          "on Customeralias.customerid = [Order]. CustomerId"+ in                                                          "where [Order]. OrderDate >=:p aradatetime and customeralias.age=:p araage;"); -                 //when querying with native SQL statements, assemble entity classes with addentity toResucustomers = Session. Createsqlquery (Sqlusingparameter). Addentity ("Customeralias",typeof(Customer)) +. SetString ("Paradatetime", DateTime) -. SetInt32 ("Paraage",Ten) the. List<customer>(); *  $ transaction.commit ();Panax Notoginseng             } -             Catch(Exception) the             { + transaction. Rollback (); A                 Throw; the             } +             finally -             { $                 //If support delay has been turned on, when externally used to defer loading properties, $                 //The session is already closed, so it throws an exception.  - session. Close (); -             } the  -             returnresucustomers;Wuyi}

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.