具体不多说了,只贴出相关源码~
using System ;
en utilisant System.Collections.Generic ;
en utilisant System.Text ;
en utilisant System.Data ;
en utilisant System.Data.OleDb ;
en utilisant System.Web ;
/**//// <résumé>
/// 名称:access下的分页方案(仿sql存储过程)
/// 作者:cncxz(虫虫)
/// blog: http://cncxz.cnblogs.com
/// </summary>
classe publique AdoPager
{
chaîne protégée m_ConnString ;
protégé OleDbConnection m_Conn ;
public AdoPager()
{
CreateConn(string.Empty);
}
public AdoPager (chaîne dbPath)
{
CreateConn(dbPath);
}
privé void CreateConn (string dbPath)
{
si (string.IsNullOrEmpty(dbPath))
{
string str = System.Configuration.ConfigurationManager.AppSettings["dbPath"] en tant que chaîne ;
si (string.IsNullOrEmpty(str))
str = "~/App_Data/db.mdb";
m_ConnString = string.Format (@"Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}", HttpContext.Current.Server.MapPath(str));
}
autre
m_ConnString = string.Format (@"Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}", dbPath);
m_Conn = nouveau OleDbConnection(m_ConnString);
}
/**//// <résumé>
/// 打开连接
/// </summary>
public vide ConnOpen()
{
si (m_Conn.State != ConnectionState.Open)
m_Conn.Open();
}
/**//// <résumé>
/// 关闭连接
/// </summary>
public void ConnClose()
{
si (m_Conn.State != ConnectionState.Closed)
m_Conn.Close();
}
chaîne privée recordID (requête de chaîne, int passCount)
{
OleDbCommand cmd = new OleDbCommand(query, m_Conn);
résultat de la chaîne = chaîne.Empty ;
en utilisant (IDataReader dr = cmd.ExecuteReader())
{
tandis que (dr.Read())
{
si (passCount <1)
{
résultat += "," + dr.GetInt32(0);
}
passCount--;
}
}
renvoie le résultat.Substring(1);
}
/**//// <résumé>
///修改一下源码吧 :)
/// </summary>
/// <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, stringwhereString, string orderString, out int pageCount, out int recordCount)
{
si (pageIndex < 1) pageIndex = 1 ;
si (pageSize < 1) pageSize = 10 ;
if (string.IsNullOrEmpty(showString)) showString = "*";
if (string.IsNullOrEmpty(orderString)) orderString = "ID desc";
ConnOpen();
string monVw = string.Format(" ( {0} ) tempVw ", queryString);
OleDbCommand cmdCount = new OleDbCommand(string.Format(" select count(0) as recordCount from {0} {1}", myVw,whereString), m_Conn);
recordCount = Convert.ToInt32(cmdCount.ExecuteScalar());
si ((recordCount % pageSize) > 0)
pageCount = recordCount / pageSize + 1 ;
autre
pageCount = recordCount / pageSize ;
OleDbCommand cmdRecord ;
if (pageIndex == 1)//第一页
{
cmdRecord = new OleDbCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw,whereString, orderString), m_Conn);
}
sinon si (pageIndex > pageCount)//超出总页数
{
cmdRecord = new OleDbCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, "where 1=2", orderString), m_Conn) ;
}
autre
{
int pageLowerBound = pageSize * pageIndex;
int pageUpperBound = pageLowerBound - pageSize;
string recordIDs = recordID(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageLowerBound, "ID", myVw,whereString, orderString), pageUpperBound);
cmdRecord = new OleDbCommand(string.Format("select {0} from {1} which id in ({2}) order by {3} ", showString, myVw, recordIDs, orderString), m_Conn);
}
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdRecord);
DataTable dt=nouveau DataTable();
dataAdapter.Fill(dt);
ConnFerme();
retourner dt ;
}
}
还有调用示例:
html ici
<%@ 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="serveur">
<title>分页演示</title>
</tête>
<corps>
<form id="form1" runat="serveur">
<div>
<br />
Valeur <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="Blanc" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="Blanc" HorizontalAlign="Centre" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="Blanc" />
<AlternatingRowStyle BackColor="Blanc" />
</asp:GridView>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</corps>
</html>
Codebehind ci-dessous
utiliser le système ;
en utilisant System.Data ;
en utilisant System.Configuration ;
en utilisant System.Web ;
en utilisant System.Web.Security ;
en utilisant System.Web.UI ;
en utilisant System.Web.UI.WebControls ;
en utilisant System.Web.UI.WebControls.WebParts ;
en utilisant System.Web.UI.HtmlControls ;
en utilisant System.Collections.Generic ;
classe partielle publique _Default : System.Web.UI.Page
{
privé AdoPager mm_Pager ;
AdoPager protégé m_Pager
{
obtenir{
si (mm_Pager == nul)
mm_Pager = nouveau AdoPager();
renvoyer mm_Pager ;
}
}
protected void Page_Load (expéditeur de l'objet, EventArgs e)
{
si(!IsPostBack)
LoadData();
}
pageIndex int privé = 1 ;
privé int pageSize = 20 ;
private int pageCount = -1 ;
private int recordCount = -1 ;
vide privé LoadData()
{
string strQuery = "sélectionnez a.*,b.KindText depuis tableTest une jointure gauche tableKind b sur a.KindCode=b.KindCode ";
string strShow = "ID, Sujet, 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);
}
protected void btnJump_Click (expéditeur de l'objet, EventArgs e)
{
int.TryParse(txtPageSize.Text, hors pageIndex);
LoadData();
}
}