具体不多说了,只贴出相关源码~
usando Sistema;
usando System.Collections.Generic;
usando System.Text;
usando System.Data;
usando System.Data.OleDb;
usando System.Web;
/**//// <resumo>
/// 名称:access下的分页方案(仿sql存储过程)
/// 作者:cncxz(虫虫)
///blog: http://cncxz.cnblogs.com
/// </sumário>
classe pública AdoPager
{
string protegida m_ConnString;
OleDbConnection protegido 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["dbPath"] como string;
if (string.IsNullOrEmpty(str))
str = "~/App_Data/db.mdb";
m_ConnString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Fonte de dados={0}", HttpContext.Current.Server.MapPath(str));
}
outro
m_ConnString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Fonte de dados={0}", dbPath);
m_Conn = novo OleDbConnection(m_ConnString);
}
/**//// <resumo>
/// 打开连接
/// </sumário>
público vazio ConnOpen()
{
if (m_Conn.State! = ConnectionState.Open)
m_Conn.Open();
}
/**//// <resumo>
/// 关闭连接
/// </sumário>
public void ConnClose()
{
if (m_Conn.State! = ConnectionState.Closed)
m_Conn.Close();
}
string privada recordID(string query, int passCount)
{
OleDbCommand cmd = novo OleDbCommand(query, m_Conn);
string resultado = string.Empty;
usando (IDataReader dr = cmd.ExecuteReader())
{
enquanto (dr.Read ())
{
if (passCount < 1)
{
resultado += "," + dr.GetInt32(0);
}
contagem de senhas--;
}
}
retornar resultado.Substring(1);
}
/**//// <resumo>
/// 获取当前页应该显示的记录,注意:查询中必须包含名为ID的自动编号列,若不符合你的要求,就修改一下源码吧 :)
/// </sumário>
/// <param name="pageIndex">当前页码</param>
/// <param name="pageSize">分页容量</param>
/// <param name="showString">显示的字段</param>
/// <param name="queryString">查询字符串,支持联合查询</param>
/// <param name="whereString">查询条件,若有条件限制则必须以where 开头</param>
/// <param name="orderString">排序规则</param>
/// <param name="pageCount">传出参数:总页数统计</param>
/// <param name="recordCount">传出参数:总记录统计</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 = "*";
if (string.IsNullOrEmpty(orderString)) orderString = "ID desc";
ConnOpen();
string meuVw = string.Format("({0}) tempVw ", queryString);
OleDbCommand cmdCount = new OleDbCommand(string.Format(" selecione contagem(0) como recordCount de {0} {1}", myVw, whereString), m_Conn);
recordCount = Convert.ToInt32(cmdCount.ExecuteScalar());
if ((recordCount % pageSize) > 0)
pageCount = recordCount / pageSize + 1;
outro
pageCount = recordCount / pageSize;
OleDbCommandcmdRecord;
if (pageIndex == 1)//第一页
{
cmdRecord = new OleDbCommand(string.Format("selecione {0} {1} superior de {2} {3} ordenar por {4} ", pageSize, showString, myVw, whereString, orderString), m_Conn);
}
else if (pageIndex > pageCount)//超出总页数
{
cmdRecord = new OleDbCommand(string.Format("selecione {0} {1} superior de {2} {3} ordenar por {4} ", pageSize, showString, myVw, "where 1=2", orderString), m_Conn) ;
}
outro
{
int pageLowerBound = pageSize * pageIndex;
int pageUpperBound = pageLowerBound - pageSize;
string recordIDs = recordID(string.Format("selecione {0} {1} superior de {2} {3} ordenar por {4} ", pageLowerBound, "ID", myVw, whereString, orderString), pageUpperBound);
cmdRecord = new OleDbCommand(string.Format("selecione {0} de {1} onde id em ({2}) ordene por {3} ", showString, myVw, recordIDs, orderString), m_Conn);
}
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdRecord);
DataTable dt=new DataTable();
dataAdapter.Fill(dt);
FecharConn();
retornar dt;
}
}
还有调用示例:
html
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml " >
<head runat="servidor">
<title>分页演示</title>
</head>
<corpo>
<form id="form1" runat="servidor">
<div>
<br />
转到第<asp:TextBox ID="txtPageSize" runat="server" Width="29px">1</asp:TextBox>页<asp:Button ID="btnJump" runat="server" Text="Go" OnClick="btnJump_Click" /><br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="90%">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Branco" />
</asp:GridView>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
示例的codebehind代码
usando o sistema;
usando System.Data;
usando System.Configuration;
usando System.Web;
usando System.Web.Security;
usando System.Web.UI;
usando System.Web.UI.WebControls;
usando System.Web.UI.WebControls.WebParts;
usando System.Web.UI.HtmlControls;
usando System.Collections.Generic;
classe parcial pública _Default: System.Web.UI.Page
{
private AdoPager mm_Pager;
AdoPager protegido m_Pager
{
pegar{
if (mm_Pager == nulo)
mm_Pager = new AdoPager();
retornar mm_Pager;
}
}
protegido void Page_Load (remetente do objeto, EventArgs e)
{
if(!IsPostBack)
CarregarDados();
}
private int pageIndex = 1;
private int pageSize = 20;
private int contagem de páginas = -1;
private int recordCount = -1;
vazio privado LoadData()
{
string strQuery = "selecione a.*,b.KindText de tableTest a left join tableKind b em a.KindCode=b.KindCode ";
string strShow = "ID,Assunto,KindCode,KindText";
DataTable dt = m_Pager.ExecutePager(pageIndex, pageSize, strShow, strQuery, "", "ID desc", out pageCount, out recordCount);
GridView1.DataSource = dt;
GridView1.DataBind();
Label1.Text = string.Format("共{0}条记录,每页{1}条,页次{2}/{3}",recordCount,pageSize,pageIndex,pageCount);
}
void protegido btnJump_Click (remetente do objeto, EventArgs e)
{
int.TryParse(txtPageSize.Text, out pageIndex);
CarregarDados();
}
}
最后附上工程文件下载地址: http://cncxz.cnblogs.com/archive/2006/06/28/438050.html