分享
 
 
 

ASP.net的ACCESS数据分页方案

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

常用ASP.net分页为 DataGrid 控件 和 ADO分页

本分页为缓存唯一标识字段 只选出分页后的记录

对大量数据库分页提高了效率

NetPage.vb

Imports System.Web

Imports System.Data

Imports System.Data.OleDb

Namespace RyNetPage

Public Class NetPage

Inherits System.Web.UI.Page

Private Conn As OleDbConnection

Private DS As DataSet

Private DT As DataTable

Private SQLstr, ScriptName As String

Private GetPage, PageCount, ListCount As Integer

Private OrderStr, TableStr, IndexStr, ColumnStr, JscriptStr As String

Private PageSizeNum As Integer

Private PageIndex As String

Public Sub New()

ScriptName = "RyNetPage"

PageSizeNum = 10

End Sub

Public WriteOnly Property ConnStr()

Set(ByVal Value)

Try

Conn = New OleDbConnection(Value)

Conn.Open()

Catch ex As Exception

Echo(".New" & ex.Message.ToString)

End Try

End Set

End Property

Public WriteOnly Property Jscript() As String

Set(ByVal Value As String)

JscriptStr = Value

End Set

End Property

Public WriteOnly Property PageSize() As String

Set(ByVal Value As String)

PageSizeNum = Convert.ToInt32(Value)

End Set

End Property

Public WriteOnly Property Order() As String

Set(ByVal Value As String)

If AppCache("Order") <> Value Then

OrderStr = "Order " & Value

AppCache("Order") = OrderStr

WriteIndex()

End If

End Set

End Property

Public WriteOnly Property Table() As String

Set(ByVal Value As String)

TableStr = "[" & Value & "]"

End Set

End Property

Public WriteOnly Property Index() As String

Set(ByVal Value As String)

IndexStr = Value

End Set

End Property

Public WriteOnly Property Column() As String

Set(ByVal Value As String)

ColumnStr = Value

End Set

End Property

Public ReadOnly Property RecordsCount() As Integer

Get

Try

WriteIndex()

DT = AppCache("Index")

Return DT.Rows.Count

Catch ex As Exception

Echo(".RecordsCount " & ex.Message.ToString)

End Try

End Get

End Property

Public Function ShowRecords() As DataTable

Try

SQLstr = "Select " & ColumnStr & " From " & TableStr & PageWhere()

WriteIndex()

Dim Adapt As OleDbDataAdapter

Adapt = New OleDbDataAdapter(SQLstr, Conn)

DS = New DataSet

DS.Clear()

Adapt.Fill(DS)

Return DS.Tables(0)

Catch ex As Exception

Echo(".ShowRecords " & ex.Message.ToString)

End Try

End Function

Public Function ShowPage(Optional ByVal GetStr As String = "") As String

Dim Temp As String

Temp = "<Script Language=""Jscript"" Src=""" & JscriptStr & """></Script>" & Chr(13)

Temp += "<Script Language=""Jscript"">Pages(" & GetPage + 1 & "," & PageCount & "," & ListCount & ",'" & GetStr & "Page')</Script>"

Return Temp

End Function

Public Sub Clear()

AppCache("Index") = Nothing

End Sub

Private Function PageWhere() As String

Try

Dim EPage As Integer

Dim i As Integer

DT = AppCache("Index")

GetPage = Convert.ToInt32(HttpContext.Current.Request.QueryString("Page"))

ListCount = RecordsCount()

PageCount = CInt(Fix(ListCount / PageSizeNum) + 1)

If ListCount Mod PageSizeNum = 0 Then

PageCount = PageCount - 1

End If

If GetPage = 0 Or PageCount < GetPage Then

GetPage = 1

End If

GetPage = GetPage - 1

If ListCount < PageSizeNum Or GetPage = PageCount Then

EPage = ListCount - 1

Else

EPage = (GetPage * PageSizeNum + PageSizeNum) - 1

End If

For i = (GetPage * PageSizeNum) To EPage

PageIndex += DT.Rows(i)(0) & ","

If i = ListCount - 1 Then Exit For

Next

PageIndex = Left(PageIndex, Len(PageIndex) - 1)

Return " Where " & IndexStr & " In(" & PageIndex & ")"

Catch ex As Exception

Echo(".PageWhere " & ex.Message.ToString)

End Try

End Function

Private Sub WriteIndex()

Try

If (AppCache("Index") Is Nothing) Then

Dim Adapt As OleDbDataAdapter

Adapt = New OleDbDataAdapter("Select " & IndexStr & " From " & TableStr & OrderStr, Conn)

DS = New DataSet

DS.Clear()

Adapt.Fill(DS)

AppCache("Index") = DS.Tables(0)

End If

Catch ex As Exception

Echo(".WriteIndex " & ex.Message.ToString)

End Try

End Sub

Private Property AppCache(ByVal SetName As String)

Get

Return HttpContext.Current.Application.Get(ScriptName & "_" & TableStr & "_" & SetName)

End Get

Set(ByVal Value)

HttpContext.Current.Application.Lock()

HttpContext.Current.Application.Set(ScriptName & "_" & TableStr & "_" & SetName, Value)

HttpContext.Current.Application.UnLock()

End Set

End Property

Private Sub Echo(ByVal Value As String)

Dispose()

HttpContext.Current.Response.Write(Value)

HttpContext.Current.Response.End()

End Sub

Protected Overrides Sub Finalize()

MyBase.Finalize()

Dispose()

End Sub

Public Overrides Sub Dispose()

If Not (Conn Is Nothing) Then

Conn.Dispose()

End If

End Sub

End Class

End Namespace

Pages.js

function Pages(requestpage,MaxPage,RecordCount,PageName){

var p,ii;

if((requestpage-1)%10==0)

p=(requestpage-1)/10;

else

p=parseInt((requestpage-1)/10);

document.write('<font class="smalltxt">');

document.write('&nbsp;'+RecordCount+'&nbsp;');

document.write('&nbsp;'+requestpage+'/'+MaxPage+'&nbsp;&nbsp;Pages&nbsp;');

if(requestpage==1)

document.write('&lt;');

else

document.write('<a href=?'+PageName+'=1 title=首页>&lt;</a>');

if(p*10>0)

{

document.write(' <a href=?'+PageName+'='+p*10+' title=上十页>..</a>');

}

for(ii=p*10+1;ii<=p*10+10;ii++)

{

if(ii==requestpage)

{

document.write (' <u><font color="#CC0033">'+ii+'</font></u> ');

}

else

{

document.write (' <a href=?'+PageName+'='+ii+'>'+ii+'</a> ');

}

if(ii==MaxPage)

break;

}

if(ii<MaxPage)

{

document.write('<a href=?'+PageName+'='+ii+' title=下十页>..</a> ');

if(requestpage==MaxPage)

document.write('&gt;');

else

document.write('<a href=?'+PageName+'='+MaxPage+' title=尾页>&gt;</a>');

}

document.write('</font>&nbsp;<input class="sBut" type="text" size="1" name="page" value="'+requestpage+'" class="PageInput">&nbsp;<input class="sBut" type="button" value="GO" onclick="window.location=\'?'+PageName+'=\'+page.value">');

}

调用示范

Dim Net = New NetPage

ConnStr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Server.MapPath("DataBase/RYBBS.mdb")

Dim i As Integer

Net.Jscript = Server.MapPath("Jscript/Pages.js") 'JS分页函数地址

Net.ConnStr = ConnStr '连接数据库

Net.PageSize = 20 '每页显示数

Net.Table = "Test" '表名

Net.Index = "ID" '唯一标识字段名

Net.Column = "ID,Title,Content" '要显示的字段名

Net.Order = "By ID Desc" '排序

Dim RS As DataTable

RS = Net.ShowRecords '调出分页后的记录

Response.Write("<table>")

For i = 0 To RS.Rows.Count - 1

Response.Write("<tr>" & Chr(13))

Response.Write("<td>" & RS.Rows(i)(0) & "</td><td>" & RS.Rows(i)(1) & "</td><td>" & RS.Rows(i)(2) & "</td>" & Chr(13))

Response.Write("</tr>" & Chr(13))

Next

Response.Write("<table>" & Chr(13))

Response.Write(Net.ShowPage("ID=20&") & "<br>") 'ID=20&为一同传递的其它分页参数

Response.Write(FormatNumber(Timer - Run, 6))

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有