6-13.asp

来自「ASP+SQL Server动态网站开发从基础到实践教程」· ASP 代码 · 共 88 行

ASP
88
字号
<html>

<head>

<title>属性编程</title>
</head>

<body>

<%

Dim ConnStr,myConn,mySQL,myRec
ConnStr="Provider=SQLOLEDB;data source=(local);initial catalog=northwind;user id=sa;password=;"  '参照表6-2的连接字符串

set myRec=Server.CreateObject ("ADODB.RecordSet")
myRec.ActiveConnection=ConnStr
myRec.open "select * from customers",,1,1  '打开记录集


if not myRec.bof and not myRec.eof then '首先判断有没有记录
response.write "<table border=1>"
'首先输出字段标题,用表格来表示
response.write "<tr><td>记录编号</td><td>客户编号</td><td>公司名称</td><td>联系人</td></tr>" 

'移动到最后一条记录
myRec.movelast
'输出最后一条记录
response.write "<tr><td>最后一条</td><td>" & myRec("customerid") & "</td><td>" & myRec("companyname") & "</td><td>" & myRec("contactname") & "</td></tr>"

'移动到倒数第二条记录,通过MovePrevious方法
'移动后要查看是否到了记录的最前头,因为有可能只有一条记录
'所以要用Bof判断
myRec.MovePrevious
if not myRec.Bof then
'输出倒数第二条记录
response.write "<tr><td>倒数第二条</td><td>" & myRec("customerid") & "</td><td>" & myRec("companyname") & "</td><td>" & myRec("contactname") & "</td></tr>"
end if

'第1条记录
myRec.moveFirst
'输出第一条记录
response.write "<tr><td>第一条</td><td>" & myRec("customerid") & "</td><td>" & myRec("companyname") & "</td><td>" & myRec("contactname") & "</td></tr>"

'移到下一条
myRec.movenext
'移动到下一条后,需要判断是否到了记录的最后头
if not myRec.Eof then
'输出第二条记录
response.write "<tr><td>第二条</td><td>" & myRec("customerid") & "</td><td>" & myRec("companyname") & "</td><td>" & myRec("contactname") & "</td></tr>"
end if

myBookMark=myRec.bookmark   '将第二条记录保存为书签

'输出第十条
'通过Move方法移动到第10条,最好在移动之前判断总体记录数
'判断记录数量可以通过RecordCout方法,在后面例题中会介绍
'注意,move后面的第1个参数是移动的数量,第二个参数是开始移动的位置
'如果不传送第二个参数,则表示的是从当前记录开始移动
'如果要传送,则只能传送书签BookMark
myRec.move 10,myBookMark  '前面已经保存了书签,因此是从第二条向后移动10条


'输出后移十条的记录
response.write "<tr><td>后移十条</td><td>" & myRec("customerid") & "</td><td>" & myRec("companyname") & "</td><td>" & myRec("contactname") & "</td></tr>"

'此时记录已经后移了十条 ,但是如果想快速的回到刚才的第二条,则可以通过使用书签
'因为刚才已经保存第二条为书签
myRec.bookmark=myBookMark

'输出书签保存的记录
response.write "<tr><td>书签记录</td><td>" & myRec("customerid") & "</td><td>" & myRec("companyname") & "</td><td>" & myRec("contactname") & "</td></tr>"

response.write "</table>"
	
else
	response.write "表格中没有记录!"
end if

myRec.close
set myRec=nothing

%>


</body>

</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?