NHibernate1.2 执行多表查询

个人写法可能有点傻···在改进先贴出来!让大家找点思路····

目的:
将两个表的内容综合到一个集合里,然后邦定到GridView 上
解决方法:
1.创建一个综合了两个实体类的一个新的实体类(这个实体类不用有hbm.xml 文件)
2.将查询到的信息赋值给新实体类
3.将实体类存放在 ArrayList 中
4.GridView 直接邦定 ArrayList

---------------------------------------------------------------------------------------------------------

对应数据库表的两个实体类

using System;

namespace Entitys
{
/// <summary>
///
///功能描述:
///开发者:
///建立时间:
///修订描述:
///进度描述:
/// </summary>
public class UserInfo
{
private int m_id;
private string m_TureName;
private int m_age;

public int id
{
get { return m_id; }
set { m_id = value; }
}

public string TureName
{
get { return m_TureName; }
set { m_TureName = value; }
}

public int age
{
get { return m_age; }
set { m_age = value; }
}

}
}

using System;
using System.Collections;
using System.Data;

namespace Entitys
{
/// <summary>
///
///功能描述:
///开发者:
///建立时间:
///修订描述:
///进度描述:
/// </summary>
public class login
{
private int m_id;
private string m_Uname;
private string m_Upwd;
private DateTime m_LastTime;

public int id
{
get { return m_id; }
set { m_id = value; }
}

public string Uname
{
get { return m_Uname; }
set { m_Uname = value; }
}

public string Upwd
{
get { return m_Upwd; }
set { m_Upwd = value; }
}

public DateTime LastTime
{
get { return m_LastTime; }
set { m_LastTime = value; }
}

}
}

//新构建的 实体类

using System;
using System.Collections.Generic;
using System.Text;
using Entitys;

namespace CommonEntitys
{
public class UserCollection
{
public UserCollection(login l,UserInfo info)
{
this.id = l.id;
this.Uname = l.Uname;
this.Upwd = l.Upwd;
this.age = info.age;
this.LastTime = l.LastTime;
this.TureName = info.TureName;
}

private int m_id;
private string m_TureName;
private int m_age;

private string m_Uname;
private string m_Upwd;
private DateTime m_LastTime;

public int id
{
get { return m_id; }
set { m_id = value; }
}

public string TureName
{
get { return m_TureName; }
set { m_TureName = value; }
}

public int 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 DateTime LastTime
{
get { return m_LastTime; }
set { m_LastTime = value; }
}
}
}

调用方法

using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using NHibernate.Engine;
using NHibernate;
using NHibernate.Expression;
using NHibernate.Cfg;

/// <summary>
/// NHibernate执行 多表查询
/// </summary>
/// <returns></returns>
public ArrayList CreateCriteriaTest3()
{
IList list = null;
ArrayList arr = new ArrayList();
try
{
string sql = "from login as a , UserInfo as b where a.id=b.id ";

session = cfg.BuildSessionFactory().OpenSession();
IQuery query = session.CreateQuery(sql);
list = query.List();

IEnumerator ie = list.GetEnumerator();
while (ie.MoveNext())
{
object[] objs = (object[])ie.Current;
login l = (login)objs[0];
UserInfo info = (UserInfo)objs[1];
UserCollection collection = new UserCollection(l, info);
arr.Add(collection);
}
}
catch (Exception ex)
{
this.m_error = ex.Message;
}
finally
{
this.session.Close();
}
return arr;
}

展现层调用

protected void Button5_Click(object sender, EventArgs e)
{
ArrayListlist = UserTools.CreateCriteriaTest3();
this.GridView1.DataSource = list;
this.GridView1.DataBind();
}

---------------------------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值