nhibernate 2.0高效数据分页DataList控件和ObjectDataSource

发布于 2024-09-16 08:58:41 字数 1052 浏览 7 评论 0 原文

我将如何使用 nHibernate 2 ObjectDataSource

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

下面是我的数据访问方法



 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;
        }

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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-09-23 08:58:41

实际上,如果您使用 ICriteria 查询,您可以使用此辅助方法在到服务器的一次往返中获取结果页面和总记录数:

    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;
    }

它会克隆您的原始条件两次:一个返回页面记录的条件和一个返回总数的条件记录计数。它还使用 IMultiCriteria 在一次往返中执行两个数据库调用。

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.

别再吹冷风 2024-09-23 08:58:41

让它工作,但是有两次调用

我将 itemCount 添加到了 ref 中:



 itemCount = (Int64)_session.CreateQuery("select count(UserId) from User")
                    .UniqueResult();

Got it to work, but two calls

I added itemCount to a ref:



 itemCount = (Int64)_session.CreateQuery("select count(UserId) from User")
                    .UniqueResult();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文