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)); IListlist = criteria.List (); 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 list = criteria.List (); 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 list = criteria.List (); 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 list = criteria.List (); dgvList.DataSource = list.ToList(); } private void tsmiNot_Click(object sender, EventArgs e) { //ICriteria criteria = session.CreateCriteria(typeof(Student)); //criteria.Add(Expression.Not()); //IList list = criteria.List (); //dgvList.DataSource = list.ToList(); } private void tsmiIsNull_Click(object sender, EventArgs e) { ICriteria criteria = session.CreateCriteria(typeof(Student)); criteria.Add(Expression.IsNull("Name")); IList list = criteria.List (); 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 stuList = critera.List (); 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 list = criteria.List (); 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 list = criteria.List (); 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 list = criteria.List (); dgvList.DataSource = list.ToList(); } }}
5.代码下载