具体不多说了,只贴出相关源码~
using System ;
en utilisant System.Collections.Generic ;
en utilisant System.Text ;
en utilisant System.Data ;
en utilisant System.Data.OleDb ;
en utilisant System.Web ;
/**////
/// 名称:access下的分页方案(仿sql存储过程)
/// 作者:cncxz(虫虫)
/// blog: http://cncxz.cnblogs.com
///
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);
}
/**////
/// 打开连接
///
public vide ConnOpen()
{
si (m_Conn.State != ConnectionState.Open)
m_Conn.Open();
}
/**////
/// 关闭连接
///
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);
}
/**////
///修改一下源码吧 :)
///
/// 当前页码
/// 分页容量
/// 显示的字段
/// 查询字符串,支持联合查询
/// 查询条件,若有条件限制则必须以where 开头
/// 排序规则
/// 传出参数:总页数统计
/// 传出参数:总记录统计
///
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" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
http://www.w3.org/1999/xhtml " >
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();
}
}