| SQL连接测试及分页处理
以下为ASP代码,文件名SQLTEST.ASP;此代码的分页处理思想同样适用于JSP\PHP等动态网页编程~
<html> <head> <title>SQL连接测试及分页处理</title> <style type="text/css"> <!-- .STYLE3 {font-size: medium; font-weight: bold; } .STYLE4 { font-size: large; font-weight: bold; } --> </style> </head>
<body>
<% dim conn '数据库连接对象 Dim rs 'RecordSet 对象 Dim dbstr '数据库文件地址 Dim sql '数据操作指令
Dim pageSize '每页显示页数 Dim pgnm '总页数 Dim page '用户请求的页面索引 Dim count '消息计数器,用来判断是否到达分页条目
'初始化消息计数器 count=0
'定义每页显示记录数 pageSize=2
'数据库文件 dbstr="student.mdb"
'SQL命令,不区分大小写 sql="select * from student"
'使用Sevrer对象的CreateObject方法建立Connection对象 Set conn=Server.CreateObject("ADODB.Connection") Set rs=Server.CreateObject("ADODB.RecordSet")
'设置Connection对象的ConnectionString conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" conn.ConnectionString=conn.ConnectionString&Server.MapPath(dbstr) 'conn.ConnectionString="DSN=student2"
'连接数据库 conn.open()
'设置rs的ActiveConnection对象 rs.ActiveConnection=conn
'设置游标类型 rs.CursorType=1
'设置锁定类型,默认为1,也就是下面代码可以省略 rs.LockType=1
'打开记录集 rs.open sql
if(rs.EOF) then Response.Write("暂时没有任何信息!") Response.end end if
rs.PageSize=pageSize pgnm=rs.PageCount page=request.QueryString("page")
if(IsEmpty(page) or cint(page)<1 or cint(page)>pgnm) then page=1 end if
rs.absolutePage=page '指定当前页码 %>
<div align="center"> <p class="STYLE4">学生信息表</p> <table width="770" border="1" align="center" id="table1"> <tr> <th scope="col"><span class="STYLE3">学号</span></th> <th scope="col"><span class="STYLE3">姓名</span></th> <th scope="col"><span class="STYLE3">年龄</span></th> <th scope="col"><span class="STYLE3">籍贯</span></th> <th scope="col"><span class="STYLE3">班号</span></th>
</tr>
<% do while(not rs.EOF and count<>pageSize) %> <tr> <!-- <td><%=rs("学号")%></td> <td><%=rs("姓名")%></td> <td><%=rs("年龄")%></td> <td><%=rs("籍贯")%></td> <td><%=rs("班号")%></td>--> <td><%=rs(0)%></td> <td><%=rs(1)%></td> <td><%=rs(2)%></td> <td><%=rs(3)%></td> <td><%=rs(4)%></td> </tr> <% rs.MoveNext() count=count+1 loop %> </table> </div> <br>
<div align="center"> <table width="770" border="1" id="table2"> <tr> <td><p align="center">共有记录<%=rs.RecordCount%>条, 共有<%=pgnm%>页, 当前处于第<%=page%>页<br> <!--以下为分页处理--> <% if page>1 then response.write "<a href="/sqltest.asp?page=1>";首页</a> " response.write "<a href="/sqltest.asp?page="" & page-1 & ">上一页</a> " end if if rs.pagecount-page>=1 then response.write "<a href="/sqltest.asp?page="" & (page+1) & ">" response.write "下一页</a> <a href="/sqltest.asp?page=""&rs.pagecount&">尾页</a>" end if %><br> <% if page<2 then response.write "首页 上一页 " else response.write "<a href="/sqltest.asp?page=1>";首页</a> " response.write "<a href="/sqltest.asp?page="" & page-1 & ">上一页</a> " end if if rs.pagecount-page<1 then response.write "下一页 尾页" else response.write "<a href="/sqltest.asp?page="" & (page+1) & ">" response.write "下一页</a> <a href="/sqltest.asp?page=""&rs.pagecount&">尾页</a>" end if %> <!--以上为分页处理--> </td> </tr> </table>
</div> <% rs.close() '关闭及释放RecordSet对象 set rs=nothing
conn.close() '关闭及释放connection对象 set conn=nothing %> </body> </html> |