|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
在经过全球个人PC市场占有90%的微软对asp.net不断优化与整合后,asp.net与微软自身平台的动用上更加的高效,加上asp.net在应用上非常容易上手,相信asp.net仍会是最多客户选用的脚本语言,并会在未来几年继续领跑。access|asp.net|分页|技能详细未几说了,只贴出相干源码~
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.OleDb;
usingSystem.Web;
/**////<summary>
///称号:access下的分页计划(仿sql存储历程)
///cncxz(虫虫)
///blog:http://cncxz.cnblogs.com
///</summary>
publicclassAdoPager
{
protectedstringm_ConnString;
protectedOleDbConnectionm_Conn;
publicAdoPager()
{
CreateConn(string.Empty);
}
publicAdoPager(stringdbPath)
{
CreateConn(dbPath);
}
privatevoidCreateConn(stringdbPath)
{
if(string.IsNullOrEmpty(dbPath))
{
stringstr=System.Configuration.ConfigurationManager.AppSettings["dbPath"]asstring;
if(string.IsNullOrEmpty(str))
str="~/App_Data/db.mdb";
m_ConnString=string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Datasource={0}",HttpContext.Current.Server.MapPath(str));
}
else
m_ConnString=string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Datasource={0}",dbPath);
m_Conn=newOleDbConnection(m_ConnString);
}
/**////<summary>
///翻开毗连
///</summary>
publicvoidConnOpen()
{
if(m_Conn.State!=ConnectionState.Open)
m_Conn.Open();
}
/**////<summary>
///封闭毗连
///</summary>
publicvoidConnClose()
{
if(m_Conn.State!=ConnectionState.Closed)
m_Conn.Close();
}
privatestringrecordID(stringquery,intpassCount)
{
OleDbCommandcmd=newOleDbCommand(query,m_Conn);
stringresult=string.Empty;
using(IDataReaderdr=cmd.ExecuteReader())
{
while(dr.Read())
{
if(passCount<1)
{
result+=","+dr.GetInt32(0);
}
passCount--;
}
}
returnresult.Substring(1);
}
/**////<summary>
///猎取以后页应当显现的纪录,注重:查询中必需包括名为ID的主动编号列,若不切合你的请求,就修正一下源码吧:)
///</summary>
///<paramname="pageIndex">以后页码</param>
///<paramname="pageSize">分页容量</param>
///<paramname="showString">显现的字段</param>
///<paramname="queryString">查询字符串,撑持团结查询</param>
///<paramname="whereString">查询前提,如有前提限定则必需以where开首</param>
///<paramname="orderString">排序划定规矩</param>
///<paramname="pageCount">传出参数:总页数统计</param>
///<paramname="recordCount">传出参数:总纪录统计</param>
///<returns>装载纪录的DataTable</returns>
publicDataTableExecutePager(intpageIndex,intpageSize,stringshowString,stringqueryString,stringwhereString,stringorderString,outintpageCount,outintrecordCount)
{
if(pageIndex<1)pageIndex=1;
if(pageSize<1)pageSize=10;
if(string.IsNullOrEmpty(showString))showString="*";
if(string.IsNullOrEmpty(orderString))orderString="IDdesc";
ConnOpen();
stringmyVw=string.Format("({0})tempVw",queryString);
OleDbCommandcmdCount=newOleDbCommand(string.Format("selectcount(0)asrecordCountfrom{0}{1}",myVw,whereString),m_Conn);
recordCount=Convert.ToInt32(cmdCount.ExecuteScalar());
if((recordCount%pageSize)>0)
pageCount=recordCount/pageSize+1;
else
pageCount=recordCount/pageSize;
OleDbCommandcmdRecord;
if(pageIndex==1)//第一页
{
cmdRecord=newOleDbCommand(string.Format("selecttop{0}{1}from{2}{3}orderby{4}",pageSize,showString,myVw,whereString,orderString),m_Conn);
}
elseif(pageIndex>pageCount)//超越总页数
{
cmdRecord=newOleDbCommand(string.Format("selecttop{0}{1}from{2}{3}orderby{4}",pageSize,showString,myVw,"where1=2",orderString),m_Conn);
}
else
{
intpageLowerBound=pageSize*pageIndex;
intpageUpperBound=pageLowerBound-pageSize;
stringrecordIDs=recordID(string.Format("selecttop{0}{1}from{2}{3}orderby{4}",pageLowerBound,"ID",myVw,whereString,orderString),pageUpperBound);
cmdRecord=newOleDbCommand(string.Format("select{0}from{1}whereidin({2})orderby{3}",showString,myVw,recordIDs,orderString),m_Conn);
}
OleDbDataAdapterdataAdapter=newOleDbDataAdapter(cmdRecord);
DataTabledt=newDataTable();
dataAdapter.Fill(dt);
ConnClose();
returndt;
}
}
另有挪用示例:html代码
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>分页演示</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<br/>
转到第<asp:TextBoxID="txtPageSize"runat="server"Width="29px">1</asp:TextBox>页<asp:ButtonID="btnJump"runat="server"Text="Go"/><br/>
<asp:GridViewID="GridView1"runat="server"CellPadding="4"ForeColor="#333333"GridLines="None"Width="90%">
<FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>
<RowStyleBackColor="#EFF3FB"/>
<EditRowStyleBackColor="#2461BF"/>
<SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/>
<PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/>
<HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>
<AlternatingRowStyleBackColor="White"/>
</asp:GridView>
</div>
<asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>
</form>
</body>
</html>
示例的codebehind代码
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Collections.Generic;
publicpartialclass_Default:System.Web.UI.Page
{
privateAdoPagermm_Pager;
protectedAdoPagerm_Pager
{
get{
if(mm_Pager==null)
mm_Pager=newAdoPager();
returnmm_Pager;
}
}
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
LoadData();
}
privateintpageIndex=1;
privateintpageSize=20;
privateintpageCount=-1;
privateintrecordCount=-1;
privatevoidLoadData()
{
stringstrQuery="selecta.*,b.KindTextfromtableTestaleftjointableKindbona.KindCode=b.KindCode";
stringstrShow="ID,Subject,KindCode,KindText";
DataTabledt=m_Pager.ExecutePager(pageIndex,pageSize,strShow,strQuery,"","IDdesc",outpageCount,outrecordCount);
GridView1.DataSource=dt;
GridView1.DataBind();
Label1.Text=string.Format("共{0}笔记录,每页{1}条,页次{2}/{3}",recordCount,pageSize,pageIndex,pageCount);
}
protectedvoidbtnJump_Click(objectsender,EventArgse)
{
int.TryParse(txtPageSize.Text,outpageIndex);
LoadData();
}
}
中间码是基于一个虚拟机器。源代码是最高层的,理论上从源代码开始直接编译成本地码能提供最大优化的。而中间码只能是转译成本地码,效率上难免受到损耗。根据虚拟机器所设定的体系结构的特点,和本地机器的差异的多少。 |
|