access下的分页方案

具体不多说了,只贴出相关源码~

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Data;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Web;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// 名称:access下的分页方案(仿sql存储过程)
InBlock.gif
/// 作者:cncxz(虫虫)
InBlock.gif
/// blog:http://cncxz.cnblogs.com
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  AdoPager
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected string m_ConnString;
InBlock.gif    
protected OleDbConnection m_Conn;
InBlock.gif
InBlock.gif    
public AdoPager()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        CreateConn(
string.Empty);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public AdoPager(string dbPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        CreateConn(dbPath);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private void CreateConn(string dbPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (string.IsNullOrEmpty(dbPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string str = System.Configuration.ConfigurationManager.AppSettings["dbPath"as string;
InBlock.gif            
if (string.IsNullOrEmpty(str))
InBlock.gif                str 
= "~/App_Data/db.mdb";
InBlock.gif            m_ConnString 
= string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}", HttpContext.Current.Server.MapPath(str));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
InBlock.gif            m_ConnString 
= string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}", dbPath);
InBlock.gif
InBlock.gif        m_Conn 
= new OleDbConnection(m_ConnString);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 打开连接
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public void ConnOpen()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (m_Conn.State != ConnectionState.Open)
InBlock.gif            m_Conn.Open();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 关闭连接
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public void ConnClose()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (m_Conn.State != ConnectionState.Closed)
InBlock.gif            m_Conn.Close();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private string recordID(string query, int passCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        OleDbCommand cmd 
= new OleDbCommand(query, m_Conn);
InBlock.gif        
string result = string.Empty;
InBlock.gif        
using (IDataReader dr = cmd.ExecuteReader())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
while (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (passCount < 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    result 
+= "," + dr.GetInt32(0);
ExpandedSubBlockEnd.gif                }

InBlock.gif                passCount
--;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
return result.Substring(1);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取当前页应该显示的记录,注意:查询中必须包含名为ID的自动编号列,若不符合你的要求,就修改一下源码吧 :)
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="pageIndex">当前页码</param>
InBlock.gif    
/// <param name="pageSize">分页容量</param>
InBlock.gif    
/// <param name="showString">显示的字段</param>
InBlock.gif    
/// <param name="queryString">查询字符串,支持联合查询</param>
InBlock.gif    
/// <param name="whereString">查询条件,若有条件限制则必须以where 开头</param>
InBlock.gif    
/// <param name="orderString">排序规则</param>
InBlock.gif    
/// <param name="pageCount">传出参数:总页数统计</param>
InBlock.gif    
/// <param name="recordCount">传出参数:总记录统计</param>
ExpandedSubBlockEnd.gif    
/// <returns>装载记录的DataTable</returns>

InBlock.gif    public DataTable ExecutePager(int pageIndex, int pageSize, string showString, string queryString, string whereString, string orderString, out int pageCount, out int recordCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (pageIndex < 1) pageIndex = 1;
InBlock.gif        
if (pageSize < 1) pageSize = 10;
InBlock.gif        
if (string.IsNullOrEmpty(showString)) showString = "*";
InBlock.gif        
if (string.IsNullOrEmpty(orderString)) orderString = "ID desc";
InBlock.gif        ConnOpen();
InBlock.gif        
string myVw = string.Format(" ( {0} ) tempVw ", queryString);
InBlock.gif        OleDbCommand cmdCount 
= new OleDbCommand(string.Format(" select count(0) as recordCount from {0} {1}", myVw, whereString), m_Conn);
InBlock.gif
InBlock.gif        recordCount 
= Convert.ToInt32(cmdCount.ExecuteScalar());
InBlock.gif
InBlock.gif        
if ((recordCount % pageSize) > 0)
InBlock.gif            pageCount 
= recordCount / pageSize + 1;
InBlock.gif        
else
InBlock.gif            pageCount 
= recordCount / pageSize;
InBlock.gif        OleDbCommand cmdRecord;
InBlock.gif        
if (pageIndex == 1)//第一页
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            cmdRecord 
= new OleDbCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, whereString, orderString), m_Conn);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if (pageIndex > pageCount)//超出总页数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            cmdRecord 
= new OleDbCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, "where 1=2", orderString), m_Conn);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int pageLowerBound = pageSize * pageIndex;
InBlock.gif            
int pageUpperBound = pageLowerBound - pageSize;
InBlock.gif            
string recordIDs = recordID(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageLowerBound, "ID", myVw, whereString, orderString), pageUpperBound);
InBlock.gif            cmdRecord 
= new OleDbCommand(string.Format("select {0} from {1} where id in ({2}) order by {3} ", showString, myVw, recordIDs, orderString), m_Conn);
InBlock.gif
ExpandedSubBlockEnd.gif        }

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

ExpandedBlockEnd.gif}

None.gif

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


示例的codebehind代码
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Collections.Generic;
None.gif
None.gif
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private AdoPager mm_Pager;
InBlock.gif    
protected AdoPager m_Pager
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gif{
InBlock.gif            
if (mm_Pager == null)
InBlock.gif                mm_Pager 
= new AdoPager();
InBlock.gif            
return mm_Pager;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(!IsPostBack)
InBlock.gif            LoadData();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
private int pageIndex = 1;
InBlock.gif    
private int pageSize = 20;
InBlock.gif    
private int pageCount = -1;
InBlock.gif    
private int recordCount = -1;
InBlock.gif
InBlock.gif    
private void LoadData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strQuery = "select a.*,b.KindText from tableTest a left join tableKind b on a.KindCode=b.KindCode ";
InBlock.gif        
string strShow = "ID,Subject,KindCode,KindText";     
InBlock.gif        
InBlock.gif        DataTable dt 
= m_Pager.ExecutePager(pageIndex, pageSize, strShow, strQuery, """ID desc"out pageCount, out recordCount);
InBlock.gif        GridView1.DataSource 
= dt;
InBlock.gif        GridView1.DataBind();
InBlock.gif        Label1.Text 
= string.Format("共{0}条记录,每页{1}条,页次{2}/{3}",recordCount,pageSize,pageIndex,pageCount);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif   
InBlock.gif    
protected void btnJump_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int.TryParse(txtPageSize.Text, out pageIndex);
InBlock.gif        LoadData();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


最后附上工程文件下载地址: http://www.cnblogs.com/Files/cncxz/AdoPager.rar
posted on 2006-06-28 19:23  cncxz(虫虫) 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/cncxz/archive/2006/06/28/438050.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值