码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-07-08 00:31:04      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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

2.效果界面

技术分享

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.代码下载

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

标签:

原文地址:http://www.cnblogs.com/2star/p/4628833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!