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)
    {
        ArrayList list = UserTools.CreateCriteriaTest3();
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();       
    }

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

 

posted @ 2007-10-18 17:00  草青工作室  阅读(119)  评论(0编辑  收藏  举报