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}