The plan distributing a page below Access
Tuesday, March 03, 2009 by rain
Specific did not say more,Stick a relevant source ~ only
Using System;Using System.Collections.Generic;Using System.Text;Using System.Data;Using System.Data.OleDb;Using System.Web;
/ **//// %26lt;summary%26gt;
/ / / name:The plan distributing a page below Access (process of copy Sql memory)
/ / / author:Cncxz(bug)
/ / / Blog:Http://cncxz.cnblogs.com
/ / / %26lt;/summary%26gt;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["dbPath"] As String;If (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));
}ElseM_ConnString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" , dbPath);
M_Conn = New OleDbConnection(m_ConnString);
}
/ **//// %26lt;summary%26gt;
/ / / open join
/ / / %26lt;/summary%26gt;Public Void ConnOpen()
{If (m_Conn.State! = ConnectionState.Open)M_Conn.Open();
}
/ **//// %26lt;summary%26gt;
/ / / close link
/ / / %26lt;/summary%26gt;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%26lt;1)
{Result += " , "+ Dr.GetInt32(0);
}PassCount- - ;
}
}Return Result.Substring(1);
}
/ **//// %26lt;summary%26gt;
/ / / the record that gets current page to should show,Notice:The automatic number that the name must include to be ID in inquiry is listed,If do not accord with your requirement,Revise next one sources:)
/ / / %26lt;/summary%26gt;
/ / / %26lt;/param of %26lt;param Name=%26quot;pageIndex%26quot;%26gt; current page number%26gt;
/ / / %26lt;/param of capacity of page of %26lt;param Name=%26quot;pageSize%26quot;%26gt; cent%26gt;
/ / / the field %26lt;/param that %26lt;param Name=%26quot;showString%26quot;%26gt; shows%26gt;
/ / / %26lt;param Name=%26quot;queryString%26quot;%26gt; inquires string,Support inquires %26lt;/param jointly%26gt;
/ / / %26lt;param Name=%26quot;whereString%26quot;%26gt; inquires a condition,If be restricted conditionally criterion must with %26lt;/param of Where begin%26gt;
/ / / %26lt;/param of regulation of %26lt;param Name=%26quot;orderString%26quot;%26gt; sort%26gt;
/ / / %26lt;param Name=%26quot;pageCount%26quot;%26gt; comes out parameter:Total page counts statistical %26lt;/param%26gt;
/ / / %26lt;param Name=%26quot;recordCount%26quot;%26gt; comes out parameter:Always record statistical %26lt;/param%26gt;
/ / / %26lt;returns%26gt; lades the DataTable%26lt;/returns of the record%26gt;Public DataTable ExecutePager(int PageIndex, int PageSize, string ShowString, string QueryString, string WhereString, string OrderString, out Int PageCount, out Int RecordCount)
{If (pageIndex%26lt;1) PageIndex = 1;If (pageSize%26lt;1) PageSize = 10;If (string.IsNullOrEmpty(showString)) ShowString = "*";If (string.IsNullOrEmpty(orderString)) OrderString = "ID Desc";
ConnOpen();String MyVw = 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());
If ((recordCount % PageSize)%26gt;0)PageCount = RecordCount / PageSize + 1;ElsePageCount = RecordCount / PageSize;
OleDbCommand CmdRecord;If (pageIndex==1)// the first page
{CmdRecord = New OleDbCommand(string.Format("select Top {0} {1} From {2} {3} Order By {4} " , pageSize, showString, myVw, whereString, orderString) , m_Conn);
}Else If (pageIndex%26gt;PageCount)// exceeds total page number
{CmdRecord = New OleDbCommand(string.Format("select Top {0} {1} From {2} {3} Order By {4} " , pageSize, showString, myVw, "Where 1=2" , orderString) , m_Conn);
}Else
{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} Where Id In ({2}) Order By {3} " , showString, myVw, recordIDs, orderString) , m_Conn);
}
OleDbDataAdapter DataAdapter = New OleDbDataAdapter(cmdRecord);
DataTable Dt=new DataTable();DataAdapter.Fill(dt);
ConnClose();Return Dt;
}
}
Still have call give typical examples:Html code
%26lt;%@ Page Language=%26quot;C#%26quot; AutoEventWireup=%26quot;true%26quot; CodeFile=%26quot;Default.aspx.cs%26quot; Inherits=%26quot;_Default%26quot; %%26gt;
%26lt; ! DOCTYPE Html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "%26gt;
%26lt;html Xmlns=%26quot;http://www.w3.org/1999/xhtml %26quot;%26gt;
%26lt;head Runat=%26quot;server %26quot;%26gt;
%26lt;title%26gt; divides a page to demonstrate %26lt;/title%26gt;
%26lt;/head%26gt;
%26lt;body%26gt;
%26lt;form Id=%26quot;form1%26quot; Runat=%26quot;server %26quot;%26gt;
%26lt;div%26gt;
%26lt;br /%26gt;
Turn to %26lt;asp:TExtBox ID=%26quot;txtPageSize%26quot; Runat=%26quot;server%26quot; Width=%26quot;29px%26quot;%26gt;1%26lt;/asp:TExtBox%26gt; page %26lt;asp:BUtton ID=%26quot;btnJump%26quot; Runat=%26quot;server%26quot; Text=%26quot;Go%26quot; OnClick=%26quot;btnJump_Click%26quot; /%26gt;%26lt;br /%26gt;
%26lt;asp:GRidView ID="GridView1" Runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="90% "%26gt;
%26lt;FooterStyle BackColor=%26quot;#507CD1%26quot; Font-Bold=%26quot;True%26quot; ForeColor=%26quot;White%26quot; /%26gt;
%26lt;RowStyle BackColor=%26quot;#EFF3FB%26quot; /%26gt;
%26lt;EditRowStyle BackColor=%26quot;#2461BF%26quot; /%26gt;
%26lt;SelectedRowStyle BackColor=%26quot;#D1DDF1%26quot; Font-Bold=%26quot;True%26quot; ForeColor=%26quot;#333333%26quot; /%26gt;
%26lt;PagerStyle BackColor=%26quot;#2461BF%26quot; ForeColor=%26quot;White%26quot; HorizontalAlign=%26quot;Center%26quot; /%26gt;
%26lt;HeaderStyle BackColor=%26quot;#507CD1%26quot; Font-Bold=%26quot;True%26quot; ForeColor=%26quot;White%26quot; /%26gt;
%26lt;AlternatingRowStyle BackColor=%26quot;White%26quot; /%26gt;
%26lt;/asp:GRidView%26gt;
%26lt;/div%26gt;
%26lt;asp:LAbel ID=%26quot;Label1%26quot; Runat=%26quot;server%26quot; Text=%26quot;Label%26quot;%26gt;%26lt;/asp:LAbel%26gt;
%26lt;/form%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;
The Codebehind code of give typical examplesUsing 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 = "select A. * , b.KindText From TableTest A Left Join TableKind B On A.KindCode=b.KindCode ";String StrShow = "ID, subject, 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(" in all {0} record,Every page {1} ,Page second {2}/{3}" , recordCount, pageSize, pageIndex, pageCount);
}
Protected Void BtnJump_Click(object Sender, eventArgs E)
{Int.TryParse(txtPageSize.Text, out PageIndex);
LoadData();
}
}
File of final attach project downloads an address:Http://www.cnblogs.com/Files/cncxz/AdoPager.rar...