NHibernate + AspNetPager 实现分页

 

NHibernate + AspNetPager 实现分页

白折腾了一天,原来NHibernate的 各种 Dll的版本不一样,配置文件经常找不到路径,

换了好几个源码都无法正常运行, 最后在CodeProject.com里找到一个思路比较清晰的Demo,

郁闷的是居然没有数据库文件,自己勉强从实体层xml还原数据库,呵呵,这会可OK了。

 

不知分页效果如何,顺便把分页也写进去,当然首选应该是 AspNetPager 吧,刚好集成一下,还不懒!

性能比较就没有测试过了,不知和Ado.Net相比,效果如何,本人一直都是用Ado.Net的,有望高手指点一下!!

 

效果图:

 NHibernate + AspNetPager 实现分页

 

Sql文件

 

USE [NTest]
GO
/****** 对象:  Table [dbo].[Customer]    脚本日期: 04/01/2011 09:31:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Customer](
    [CustomerID]  [int] NOT NULL,
    [AccountNumber] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
    [CustomerType] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
    [TerritoryID] [int] NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
    [CustomerID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
 

 

 

NHibernateDataProvider.cs

 

 分页函数

/// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="pageStart"></param>
        /// <param name="pageLimit"></param>
        /// <returns></returns>
        public IList<Customer> GetCustomerPageModel(int pageStart, int pageLimit)
        {
            //HQL查询
            IList<Customer> customerList = GetSession().CreateQuery("from Customer")
                .SetFirstResult(pageStart * pageLimit)
                .SetMaxResults(pageLimit)
                .List<Customer>();
            return customerList;

            //条件查询
            //return Session.CreateCriteria(typeof(Customer))
            //    .SetProjection(Projections.ProjectionList()
            //    .Add(Projections.Property("Id"), "Id")
            //    .Add(Projections.Property("Name"), "Name")
            //    .Add(Projections.Property("Tel"), "Tel")
            //    .Add(Projections.Property("Address"), "Address")
            //    .Add(Projections.Property("Sex"), "Sex")
            //    .Add(Projections.Property("CreateDate"), "CreateDate"))
            //    .AddOrder(Order.Asc("Id"))
            //    .SetFirstResult(pageStart * pageLimit)
            //    .SetMaxResults(pageLimit)
            //    .SetResultTransformer(Transformers.AliasToBean(typeof(Customer)))
            //    .List<Customer>();

            //SQL查询
            //IList<Customer> customerList = Session.CreateSQLQuery("SELECT * FROM Customer")
            //    .SetFirstResult(pageStart*pageLimit)
            //    .SetMaxResults(pageLimit)
            //    .SetResultTransformer(Transformers.AliasToBean<Customer>()).List<Customer>();
            //return customerList;
        }

 

 

 Default.aspx
页面代码

 

 

<form id="form1" runat="server">
    <div style="margin: 40px; border: 1px solid #ccc;">
        <h3>
            NHibernate Get
        </h3>
        <ul>
            <li>
                <%=CustomerId %></li>
            <li>
                <%=Firstname %></li>
            <li>
                <%=Lastname %></li>
        </ul>
        <h3>
            NHibernate IQuery
        </h3>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
        </asp:GridView>
        <div class="pagingBar">
            <cc1:AspNetPager ID="AspNetPager1" runat="server"  PageSize="1"
                onpagechanging="AspNetPager1_PageChanging">
            </cc1:AspNetPager>
        </div>
    </div>

 

 

 Default.aspx.cs

 

网页后置代码

 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using NHibernate.Cfg;
using NHibernate;
using DataTransfer;
using DataAccessLayer;


public partial class _Default : System.Web.UI.Page
{
    public string CustomerId;
    public string Firstname;
    public string Lastname;

    protected NHibernateDataProvider provider = new NHibernateDataProvider();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initData();
        }
    }

    public void initData()
    {
        Customer model = provider.GetCustomerById(1);
        CustomerId = model.CustomerId.ToString();
        Firstname = model.Firstname;
        Lastname = model.Lastname;

        IList<Customer> list = provider.GetAllCustomers();
        AspNetPager1.RecordCount = list.Count;

        IList<Customer> user = provider.GetCustomerPageModel(AspNetPager1.CurrentPageIndex-1, AspNetPager1.PageSize);

        GridView1.DataSource = user;
        GridView1.DataBind();
    }

    protected void AspNetPager1_PageChanging(object src, FrameWork.WebControls.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        initData();
    }
}

 

 

你可能感兴趣的:(Hibernate)