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

转载于:https://www.cnblogs.com/kimisme/p/4628833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值