NHibernate系列学习(三)-条件查询Criteria

1.本笔记主要介绍Criteria的使用

2.效果界面

image

3.代码详情

namespace KimismeDemo

{

    public partial class Form3 : Form

    {

        private ISession session;

        private ISessionFactory factory;

        private ITransaction trans;

        public Form3()

        {

            InitializeComponent();

        }



        #region 0.0 初始化NH - private void Form3_Load(object sender, EventArgs e)

        private void Form3_Load(object sender, EventArgs e)

        {

            Configuration config = new Configuration().AddAssembly("Kimisme");

            factory = config.BuildSessionFactory();

            session = factory.OpenSession();

            dgvList.AutoGenerateColumns = false;

        } 

        #endregion



        private void tsmiEqual_Click(object sender, EventArgs e)

        {

            ICriteria criteria = session.CreateCriteria(typeof (Student));

            criteria.Add(Expression.Eq("Age", 10));

            IList<Student> list = criteria.List<Student>();

            dgvList.DataSource = list.ToList();

            

        }



        private void tsmiGreaterThan_Click(object sender, EventArgs e)

        {

            ICriteria criteria = session.CreateCriteria(typeof(Student));

            criteria.Add(Expression.Gt("Age",10));

            IList<Student> list = criteria.List<Student>();

            dgvList.DataSource = list.ToList();

        }



        private void tsmiLessThan_Click(object sender, EventArgs e)

        {

            ICriteria criteria = session.CreateCriteria(typeof(Student));

            criteria.Add(Expression.Lt("Age", 10));

            IList<Student> list = criteria.List<Student>();

            dgvList.DataSource = list.ToList();

        }



        private void tsmiLike_Click(object sender, EventArgs e)

        {

            ICriteria criteria = session.CreateCriteria(typeof(Student));

            criteria.Add(Expression.Gt("Name", "i"));

            IList<Student> list = criteria.List<Student>();

            dgvList.DataSource = list.ToList();

        }



        private void tsmiNot_Click(object sender, EventArgs e)

        {

            //ICriteria criteria = session.CreateCriteria(typeof(Student));

            //criteria.Add(Expression.Not());

            //IList<Student> list = criteria.List<Student>();

            //dgvList.DataSource = list.ToList();

        }



        private void tsmiIsNull_Click(object sender, EventArgs e)

        {

            ICriteria criteria = session.CreateCriteria(typeof(Student));

            criteria.Add(Expression.IsNull("Name"));

            IList<Student> list = criteria.List<Student>();

            dgvList.DataSource = list.ToList();

        }



        private void tsmiMultiQueryNew_Click(object sender, EventArgs e)

        {

            ICriteria critera = session.CreateCriteria(typeof (Student));

            Student stu =new Student();

            stu.Name = "Kim";

            stu.Age = 18;

            critera.Add(Example.Create(stu));

            IList<Student> stuList = critera.List<Student>();

            dgvList.DataSource = stuList.ToList();

        }



        private void tsmiMultiQueryCriteria_Click(object sender, EventArgs e)

        {

           IList stuList = session.CreateCriteria(typeof (Student))

                .Add(Expression.Like("Name", "%i%"))

                .Add(Expression.Between("Age", 17, 19))

                .List();

            dgvList.DataSource = stuList;

        }



        private void tsmiOr_Click(object sender, EventArgs e)

        {

            ICriteria criteria = session.CreateCriteria(typeof(Student));

            criteria.Add(Expression.Or(Expression.Eq("Name","Kim"),Expression.Eq("Name","Lily")));

            IList<Student> list = criteria.List<Student>();

            dgvList.DataSource = list.ToList();

        }



        private void tsmiBetween_Click(object sender, EventArgs e)

        {

            ICriteria criteria = session.CreateCriteria(typeof(Student));

            criteria.Add(Expression.Between("Age",17,18));

            IList<Student> list = criteria.List<Student>();

            dgvList.DataSource = list.ToList();

        }



        private void tsmiIn_Click(object sender, EventArgs e)

        {

            ICriteria criteria = session.CreateCriteria(typeof(Student));

            criteria.Add(Expression.In("Name",new string[]{"Kim","Lily"}));

            IList<Student> list = criteria.List<Student>();

            dgvList.DataSource = list.ToList();

        }

    }

}

5.代码下载

你可能感兴趣的:(Hibernate)