NHibernate1.2 执行sql 邦定GridView!!!!!!!!!!!!!
-----------实体类(不是映射的自己封装的)---------------
using NHibernate.Engine;
using System;
using System.Collections.Generic;
using System.Text;
//图方便我这里都写成了 string 类型了
namespace CommonEntitys
{
public class UserTest
{
public UserTest()
{
}
private string m_id;
private string m_TureName;
private string m_age;
private string m_Uname;
private string m_Upwd;
private string m_LastTime;
public string id
{
get { return m_id; }
set { m_id = value; }
}
public string TureName
{
get { return m_TureName; }
set { m_TureName = value; }
}
public string age
{
get { return m_age; }
set { m_age = value; }
}
public string Uname
{
get { return m_Uname; }
set { m_Uname = value; }
}
public string Upwd
{
get { return m_Upwd; }
set { m_Upwd = value; }
}
public string LastTime
{
get { return m_LastTime; }
set { m_LastTime = value; }
}
}
}
-----------方法--------------
using NHibernate.Engine;
using NHibernate;
using NHibernate.Expression;
using NHibernate.Cfg;
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 执行 sql
/// </summary>
/// <returns></returns>
public ArrayList SqlCommand()
{
ArrayList list = new ArrayList();
ISessionFactoryImplementor imp = (ISessionFactoryImplementor)cfg.BuildSessionFactory();
IDbConnection conn = imp.OpenConnection();
IDbCommand cmd = imp.ConnectionProvider.GetConnection().CreateCommand();
try
{
cmd.Connection = conn;
/ /注意下面的 login 和 UserInfo 为映射文件
cmd.CommandText = "select * from login a,userinfo b where a.id=b.id";
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 Button9_Click(object sender, EventArgs e)
{
ArrayList list = UserTools.SqlCommand();
this.GridView1.DataSource = list;
this.GridView1.DataBind();
}