Nhibbench (2)-hql

Source: Internet
Author: User
The following is a simple example.
Add two methods to obtain the dataset
Modify idal Code
Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Idal
{
Public   Interface Animalsidal
{
Bool Add (model. Animals );
Ilist < Model. Animals > Getlist ();
Ilist < Model. Animals > Getlistbyid ( Int ID );
}
}

Implement the following Dal:

Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using Nhib.pdf;
Namespace Dal
{
Public   Class Animalsdal: idal. animalsidal
{
Isession session;
Public Animalsdal ()
{
Session = Sessionmanger. getisession ();
}
Public   Bool Add (model. Animals animal)
{
Bool Flag =   False ;
Itransaction tran = Session. begintransaction ();
Try
{
Session. Save (animal );
Tran. Commit ();
Flag =   True ;
}
Catch (Exception ex ){
Tran. rollback ();
Throw Ex;
}
Return Flag;
}

Public Ilist < Model. Animals > Getlist ()
{
Return Session. createquery ( " From animals a order by A. animalid DESC " ). List < Model. Animals > ();
// Like usage
// Return session. createquery ("from animals a where. animaltype like: Type "). setstring ("type", "% BB % "). list <model. animals> ();
}
Public Ilist < Model. Animals > Getlistbyid ( Int ID)
{
Return Session. createquery ( " From animals a where a. animalid =: Aid " )
. Setint32 ( " Aid " , ID)
. List < Model. Animals > ();
}
}
}

Hql is used here.

Bll:

Code
Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Bll
{
Public Class Animalsbll
{
Idal. animalsidal aidal;
Public Animalsbll ()
{
Aidal = Factory. dalfactory. careateanimalidal ();
}
Public   Bool Add (model. Animals animal)
{
Return Aidal. Add (animal );
}

PublicIlist<Model. Animals>Getlist ()
{
ReturnAidal. getlist ();
}

PublicIlist<Model. Animals>Getlistbyid (IntID)
{
ReturnAidal. getlistbyid (ID );
}

}
}

Presentation Layer: bind the query result to the gridview

Code
Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using Bll;
Using Model;
Public   Partial   Class _ Default: system. Web. UI. Page
{
Animalsbll animalbll =   New Animalsbll ();
Protected   Void Page_load ( Object Sender, eventargs E)
{
If ( ! Ispostback)
{
This . Gridview1.datasource = Animalbll. getlist ();
This . Gridview1.databind ();
}
}
Protected   Void Ddd_click ( Object Sender, eventargs E)
{
// Animals A = new animals ();
// A. animaltype = "pig1236 ";
// Bool B = animalbll. Add ();
Int ID = Convert. toint32 ( This . Textbox1.text. Trim ());
This . Gridview1.datasource = Animalbll. getlistbyid (ID );
This . Gridview1.databind ();
}
}

 

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.