正文

access下仿Sql存储过程的分页方案2008-08-29 13:37:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yzrj/36891.html

分享到:

 

CODE:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Web;

/**//**//**//**//**//// <summary>
///
名称:access下的分页方案(仿sql存储过程)
///
作者:cncxz(虫虫)
/// from
http://bbs.51aspx.com
/// </summary>
public class AdoPager
{
    protected string m_ConnString;
    protected OleDbConnection m_Conn;

    public AdoPager()
    {
        CreateConn(string.Empty);
    }
    public AdoPager(string dbPath)
    {
        CreateConn(dbPath);
    }

    private void CreateConn(string dbPath)
    {
        if (string.IsNullOrEmpty(dbPath))
        {
            string str = System.Configuration.ConfigurationManager.AppSettings[&quot;dbPath&quot;] as string;
            if (string.IsNullOrEmpty(str))
                str = &quot;~/App_Data/db.mdb&quot;;
            m_ConnString = string.Format(@&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}&quot;, HttpContext.Current.Server.MapPath(str));
        }
        else
            m_ConnString = string.Format(@&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}&quot;, dbPath);

        m_Conn = new OleDbConnection(m_ConnString);
    }
    /**//**//**//**//**//// <summary>
    ///
打开连接
    /// </summary>
    public void ConnOpen()
    {
        if (m_Conn.State != ConnectionState.Open)
            m_Conn.Open();
    }
    /**//**//**//**//**//// <summary>
    ///
关闭连接
    /// </summary>
    public void ConnClose()
    {
        if (m_Conn.State != ConnectionState.Closed)
            m_Conn.Close();
    }

    private string recordID(string query, int passCount)
    {
        OleDbCommand cmd = new OleDbCommand(query, m_Conn);
        string result = string.Empty;
        using (IDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                if (passCount < 1)
                {
                    result += &quot;,&quot; + dr.GetInt32(0);
                }
                passCount--;
            }
        }
        return result.Substring(1);
    }


    /**//**//**//**//**//// <summary>
    ///
获取当前页应该显示的记录,注意:查询中必须包含名为ID的自动编号列,若不符合你的要求,就修改一下源码吧 :)
    /// </summary>
    /// <param name=&quot;pageIndex&quot;>
当前页码</param>
    /// <param name=&quot;pageSize&quot;>
分页容量</param>
    /// <param name=&quot;showString&quot;>
显示的字段</param>
    /// <param name=&quot;queryString&quot;>
查询字符串,支持联合查询</param>
    /// <param name=&quot;whereString&quot;>
查询条件,若有条件限制则必须以where 开头</param>
    /// <param name=&quot;orderString&quot;>
排序规则</param>
    /// <param name=&quot;pageCount&quot;>
传出参数:总页数统计</param>
    /// <param name=&quot;recordCount&quot;>
传出参数:总记录统计</param>
    /// <returns>
装载记录的DataTable</returns>
    public DataTable ExecutePager(int pageIndex, int pageSize, string showString, string queryString, string whereString, string orderString, out int pageCount, out int recordCount)
    {
        if (pageIndex < 1) pageIndex = 1;
        if (pageSize < 1) pageSize = 10;
        if (string.IsNullOrEmpty(showString)) showString = &quot;*&quot;;
        if (string.IsNullOrEmpty(orderString)) orderString = &quot;ID desc&quot;;
        ConnOpen();
        string myVw = string.Format(&quot; ( {0} ) tempVw &quot;, queryString);
        OleDbCommand cmdCount = new OleDbCommand(string.Format(&quot; select count(0) as recordCount from {0} {1}&quot;, myVw, whereString), m_Conn);

        recordCount = Convert.ToInt32(cmdCount.ExecuteScalar());

        if ((recordCount % pageSize) > 0)
            pageCount = recordCount / pageSize + 1;
        else
            pageCount = recordCount / pageSize;
        OleDbCommand cmdRecord;
        if (pageIndex == 1)//
第一页
        {
            cmdRecord = new OleDbCommand(string.Format(&quot;select top {0} {1} from {2} {3} order by {4} &quot;, pageSize, showString, myVw, whereString, orderString), m_Conn);
        }
        else if (pageIndex > pageCount)//
超出总页数
        {
            cmdRecord = new OleDbCommand(string.Format(&quot;select top {0} {1} from {2} {3} order by {4} &quot;, pageSize, showString, myVw, &quot;where 1=2&quot;, orderString), m_Conn);
        }
        else
        {
            int pageLowerBound = pageSize * pageIndex;
            int pageUpperBound = pageLowerBound - pageSize;
            string recordIDs = recordID(string.Format(&quot;select top {0} {1} from {2} {3} order by {4} &quot;, pageLowerBound, &quot;ID&quot;, myVw, whereString, orderString), pageUpperBound);
            cmdRecord = new OleDbCommand(string.Format(&quot;select {0} from {1} where id in ({2}) order by {3} &quot;, showString, myVw, recordIDs, orderString), m_Conn);

        }
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdRecord);
        DataTable dt=new DataTable();
        dataAdapter.Fill(dt);
        ConnClose();
        return dt;
    }
}




 

CODE:


<%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot;  CodeFile=&quot;Default.aspx.cs&quot; Inherits=&quot;_Default&quot; %>
<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>
<html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; >
<head runat=&quot;server&quot;>
    <title>
分页演示</title>
</head>
<body>
    <form id=&quot;form1&quot; runat=&quot;server&quot;>
    <div>
        <br />
         
转到第<asp:TextBox ID=&quot;txtPageSize&quot; runat=&quot;server&quot; Width=&quot;29px&quot;>1</asp:TextBox><asp:Button ID=&quot;btnJump&quot; runat=&quot;server&quot; Text=&quot;Go&quot; OnClick=&quot;btnJump_Click&quot; /><br />
        <asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot; CellPadding=&quot;4&quot; ForeColor=&quot;#333333&quot; GridLines=&quot;None&quot; Width=&quot;90%&quot;>
            <FooterStyle BackColor=&quot;#507CD1&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; />
            <RowStyle BackColor=&quot;#EFF3FB&quot; />
            <EditRowStyle BackColor=&quot;#2461BF&quot; />
            <SelectedRowStyle BackColor=&quot;#D1DDF1&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;#333333&quot; />
            <PagerStyle BackColor=&quot;#2461BF&quot; ForeColor=&quot;White&quot; HorizontalAlign=&quot;Center&quot; />
            <HeaderStyle BackColor=&quot;#507CD1&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; />
            <AlternatingRowStyle BackColor=&quot;White&quot; />
        </asp:GridView>
   
    </div>
        <asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;Label&quot;></asp:Label>
    </form>
</body>
</html>




 

CODE:


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

public partial class _Default : System.Web.UI.Page 
{
    private AdoPager mm_Pager;
    protected AdoPager m_Pager
    { 
        get{
            if (mm_Pager == null)
                mm_Pager = new AdoPager();
            return mm_Pager;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            LoadData();
    }
    private int pageIndex = 1;
    private int pageSize = 20;
    private int pageCount = -1;
    private int recordCount = -1;

    private void LoadData()
    {
        string strQuery = &quot;select a.*,b.KindText from tableTest a left join tableKind b on a.KindCode=b.KindCode &quot;;
        string strShow = &quot;ID,Subject,KindCode,KindText&quot;;     
       
        DataTable dt = m_Pager.ExecutePager(pageIndex, pageSize, strShow, strQuery, &quot;&quot;, &quot;ID desc&quot;, out pageCount, out recordCount);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Label1.Text = string.Format(&quot;
{0}条记录,每页{1}条,页次{2}/{3}&quot;,recordCount,pageSize,pageIndex,pageCount);
    }
   
   
    protected void btnJump_Click(object sender, EventArgs e)
    {
        int.TryParse(txtPageSize.Text, out pageIndex);
        LoadData();
    }
}



 

阅读(2456) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册