NHibernate 1.2 执行 视图(View)
using NHibernate.Engine;
using NHibernate;
public class UserAccess
{
public UserAccess()
{
cfg.AddAssembly("Entitys");
//session = cfg.BuildSessionFactory().OpenSession();
}
private NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
private ISession session = null;
private ITransaction tran = null;
/// <summary>
/// NHibernate 调用视图
/// </summary>
/// <returns></returns>
public ArrayList ExeView()
{
ArrayList list = new ArrayList();
ISessionFactoryImplementor imp = (ISessionFactoryImplementor)cfg.BuildSessionFactory();
IDbConnection conn = imp.OpenConnection();
IDbCommand cmd = imp.ConnectionProvider.GetConnection().CreateCommand();
try
{
cmd.Connection = conn;
cmd.CommandText = "select * from LogAndUser"; //LogAndUser 为视图名称
IDataReader read = cmd.ExecuteReader();
UserTest user;
while (read.Read())
{
user = new UserTest();
user.age = read.GetValue(0).ToString();
user.id = read.GetValue(1).ToString();
user.LastTime = read.GetValue(2).ToString();
user.TureName = read.GetValue(3).ToString();
user.Uname = read.GetValue(4).ToString();
//user.Upwd = read.GetValue(5).ToString();
list.Add(user);
}
}
catch (Exception ex)
{
this.m_error = ex.Message;
}
finally
{
imp.CloseConnection(conn);
}
return list;
}
}
调用方法
protected void Button7_Click(object sender, EventArgs e)
{
ArrayList list = UserTools.ExeView();
this.GridView1.DataSource = list;
this.GridView1.DataBind();
}