Nhibernate(2)--hql
接着上面的简单例子
添加两个获得数据集的方法
修改一下IDAL
实现一下DAL:
添加两个获得数据集的方法
修改一下IDAL
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace IDAL
{
public interface AnimalsIDAL
{
bool Add(Model.Animals a);
IList<Model.Animals> GetList();
IList<Model.Animals> GetListById(int id);
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace IDAL
{
public interface AnimalsIDAL
{
bool Add(Model.Animals a);
IList<Model.Animals> GetList();
IList<Model.Animals> GetListById(int id);
}
}
实现一下DAL:
Code
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
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 的用法
//return session.CreateQuery("from Animals a where a.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>();
}
}
}
这里面用到了hqlusing System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
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 的用法
//return session.CreateQuery("from Animals a where a.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>();
}
}
}
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);
}
public IList<Model.Animals> Getlist()
{
return aidal.GetList();
}
public IList<Model.Animals> GetListbyid(int id)
{
return aidal.GetListById(id);
}
}
}
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);
}
public IList<Model.Animals> Getlist()
{
return aidal.GetList();
}
public IList<Model.Animals> GetListbyid(int id)
{
return aidal.GetListById(id);
}
}
}
表现层:把查询结果绑定到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(a);
int id = Convert.ToInt32(this.TextBox1.Text.Trim());
this.GridView1.DataSource = animalbll.GetListbyid(id);
this.GridView1.DataBind();
}
}
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(a);
int id = Convert.ToInt32(this.TextBox1.Text.Trim());
this.GridView1.DataSource = animalbll.GetListbyid(id);
this.GridView1.DataBind();
}
}