本文介绍了NHibernate的2.0高效的数据分页DataList控件和ObjectDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么会做什么斯科特在一次调用使用完成的 NHibernate的2 ObjectDataSource控件

下面是我的数据访问方法

  公共IList的GetListOfUser(INT行,诠释的PageIndex){
            IList的用户列表= NULL;            使用(ITransaction TX = _session.BeginTransaction()){
                尝试{
                    用户列表= _session.CreateQuery(从用户选择U U其中,u.DateSubmitted不为空)
                        .SetFirstResult(行*(的PageIndex - 1)+ 1)的
                        .SetMaxResults(行)
                        .LIST();
                    tx.Commit();
                }赶上(NHibernate.HibernateException前){
                    tx.Rollback();
                    AppUtil.LogHelper.WriteLog(LogLevel.ERROR,ex.ToString());
                    扔;
                }
            }
            返回用户列表;
        }


解决方案

其实,你可以得到的结果页面,并使用这个辅助方法在一个往返到服务器的总记录数,如果您使用的ICriteria查询:

 保护的IList< T> GetByCriteria(
        的ICriteria标准,
        INT的PageIndex,
        INT的pageSize,
        出长TOTALCOUNT)
    {
        的ICriteria recordsCriteria = CriteriaTransformer.Clone(标准);        //分页。
        recordsCriteria.SetFirstResult(PageIndex的* pageSize的);
        recordsCriteria.SetMaxResults(pageSize的);        //计数的标准。
        的ICriteria countCriteria = CriteriaTransformer.TransformToRowCount(标准);        //执行多标准,同时获得结果,并在一次旅行到数据库计数。
        IMul​​tiCriteria多标准= Session.CreateMultiCriteria();
        multiCriteria.Add(recordsCriteria);
        multiCriteria.Add(countCriteria);
        IList的multiResult = multiCriteria.List();        IList的untypedRecords = multiResult [0]作为IList的;
        IList的< T>记录=新的List< T>();
        如果(untypedRecords!= NULL)
        {
            的foreach(在untypedRecords牛逼OBJ)
            {
                records.Add(OBJ);
            }
        }
        其他
        {
            记录=新的List< T>();
        }        TOTALCOUNT = Convert.ToInt64(((IList的)multiResult [1])[0]);        返回记录;
    }

据克隆原来的两倍标准:一是标准的返回页面的记录及总记录计数为一标准。它还使用IMultiCriteria在一个往返执行两个数据库调用。

How would I do what Scott has done in one call using nHibernate 2 ObjectDataSource

http://weblogs.asp.net/scottgu/archive/2006/01/07/434787.aspx

below is my Data access method



 public IList GetListOfUser(int rows, int pageIndex) {
            IList userList = null;

            using (ITransaction tx = _session.BeginTransaction()) {
                try {
                    userList = _session.CreateQuery("Select u from User u where u.DateSubmitted is not null")
                        .SetFirstResult(rows * (pageIndex - 1) + 1)
                        .SetMaxResults(rows)
                        .List();
                    tx.Commit();
                } catch (NHibernate.HibernateException ex) {
                    tx.Rollback();
                    AppUtil.LogHelper.WriteLog(LogLevel.ERROR, ex.ToString());
                    throw;
                }
            }
            return userList;
        }
解决方案

Actually, you can get the result page AND total records count in one roundtrip to the server using this helper method if you are using ICriteria queries:

    protected IList<T> GetByCriteria(
        ICriteria criteria, 
        int pageIndex,
        int pageSize, 
        out long totalCount)
    {
        ICriteria recordsCriteria = CriteriaTransformer.Clone(criteria);

        // Paging.
        recordsCriteria.SetFirstResult(pageIndex * pageSize);
        recordsCriteria.SetMaxResults(pageSize);

        // Count criteria.
        ICriteria countCriteria = CriteriaTransformer.TransformToRowCount(criteria);

        // Perform multi criteria to get both results and count in one trip to the database.
        IMultiCriteria multiCriteria = Session.CreateMultiCriteria();
        multiCriteria.Add(recordsCriteria);
        multiCriteria.Add(countCriteria);
        IList multiResult = multiCriteria.List();

        IList untypedRecords = multiResult[0] as IList;
        IList<T> records = new List<T>();
        if (untypedRecords != null)
        {
            foreach (T obj in untypedRecords)
            {
                records.Add(obj);
            }
        }
        else
        {
            records = new List<T>();
        }

        totalCount = Convert.ToInt64(((IList)multiResult[1])[0]);

        return records;
    }

It clone your original criteria twice: one criteria that return the records for the page and one criteria for total record count. It also uses IMultiCriteria to perform both database calls in one roundtrip.

这篇关于NHibernate的2.0高效的数据分页DataList控件和ObjectDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 00:20