NHibernate Series Learning (ii)-Using SQL and HQL and LINQ

Source: Internet
Author: User

1. This article mainly introduces NH's three ways of querying

2. Interface View

3. Code Architecture

4. Code details

namespacekimismedemo{ Public Partial classForm2:form {PrivateISession session; PrivateIsessionfactory Factory; PrivateITransaction Trans;  PublicForm2 () {InitializeComponent (); }        #region1. Initialize nh-private void Form2_load (object sender, EventArgs e)Private voidForm2_load (Objectsender, EventArgs e) {Configuration config=NewConfiguration (). Addassembly ("Kimisme"); Factory=CONFIG.            Buildsessionfactory (); Session=Factory.            Opensession (); Dgvlist.autogeneratecolumns=false; }        #endregion        #region2.0 Execute SQL statement-private void Tsmiexecutesql_click (object sender, EventArgs e)Private voidTsmiexecutesql_click (Objectsender, EventArgs e) {            stringstrSQL ="SELECT * from t_student where sId >1"; Isqlquery sqlquery= Session. Createsqlquery (strSQL). Addentity (typeof(Student)); IList<Student> stulist = sqlquery.list<student>(); Dgvlist.datasource=stulist.tolist (); }         #endregion        #region3.0 Execute stored Procedure-private void Btnexecutestoreproc_click (object sender, EventArgs e)Private voidBtnexecutestoreproc_click (Objectsender, EventArgs e) {Trans=session.            BeginTransaction (); IList<Student> stulist =NewList<student>(); Isessionfactoryimplementor Imp= Factory asIsessionfactoryimplementor; IDbConnection Conn=Imp.            Connectionprovider.getconnection (); IDbCommand cmd=Imp. Connectionprovider.getconnection ().            CreateCommand (); Try{Cmd.commandtext="pro_getstudent"; Cmd.commandtype=CommandType.StoredProcedure; IDbDataParameter parameter=cmd.                CreateParameter (); Parameter. ParameterName="StudentID"; Parameter. Value=4; Cmd.                Parameters.Add (parameter); Cmd. Connection=Conn; IDataReader Read=cmd.                ExecuteReader ();  while(read. Read ()) {Student Stu=NewStudent (); Stu. Id=int. Parse (read. GetValue (0).                    ToString ()); Stu. Name= read. GetValue (1).                    ToString (); Stu. Age=int. Parse (read. GetValue (2).                    ToString ());                Stulist.add (Stu);                } trans.commit (); Dgvlist.datasource=stulist.tolist (); }            Catch(Exception ex) {MessageBox.Show (ex).            Message); }        }         #endregion        #region4.0 Execute HQL statement-private void Tsmiexecutehql_click (object sender, EventArgs e)Private voidTsmiexecutehql_click (Objectsender, EventArgs e) {            stringSTRHQL ="From Student Stu where Stu. Id >:stuid"; IList<Student> stulist = session. CreateQuery (STRHQL). SetInt32 ("Stuid",7). List<student>(); Dgvlist.datasource=stulist.tolist (); }         #endregion        #region5.0 executing LINQ statements-aggregate functions-MaxPrivate voidTsmimax_click (Objectsender, EventArgs e) {            varStulist = Session. Queryover<student> (). Where (s = s.id >0).            List (); intMaxAge = ( fromSinchStulistSelects.age).            Max ();        MessageBox.Show (Maxage.tostring ()); }         #endregion        #region5.1 Executing LINQ statements-aggregate functions-MinPrivate voidTsmimin_click (Objectsender, EventArgs e) {            varStulist = Session. Queryover<student> (). Where (s = s.id >0).            List (); intMinage = ( fromSinchStulistSelects.age).            Min ();        MessageBox.Show (Minage.tostring ()); }         #endregion        #region5.2 Execute LINQ Statement-aggregate Function-AVGPrivate voidTsmiavg_click (Objectsender, EventArgs e) {            varStulist = Session. Queryover<student> (). Where (s = s.id >0).            List (); DoubleAvgage = ( fromSinchStulistSelects.age).            Average ();        MessageBox.Show (Avgage.tostring ()); }         #endregion        #region5.3 Execute LINQ Statement-aggregate Function-SumPrivate voidTsmisum_click (Objectsender, EventArgs e) {            varStulist = Session. Queryover<student> (). Where (s = s.id >0).            List (); intSumage = ( fromSinchStulistSelects.age).            Sum ();        MessageBox.Show (Sumage.tostring ()); }         #endregion        #region5.4 Execute LINQ Statement-aggregate Function-CountPrivate voidTsmicount_click (Objectsender, EventArgs e) {            varStulist = Session. Queryover<student> (). Where (s = s.id >0).            List (); intCountage = ( fromSinchStulistSelects.age).            Count ();        MessageBox.Show (Countage.tostring ()); }         #endregion    }}

5. Code download

NHibernate Series Learning (ii)-Using SQL and HQL and LINQ

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.