`
haimav
  • 浏览: 54311 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

connection对象和recordset对象

阅读更多

Connection对象
Ole  db 连接数据库
连接sql数据库
<%
Dim ConnStr,myConn,mySQL
ConnStr="Provider=SQLOLEDB;data source=(local);initial catalog=pubs;user id=sa;password=;"  
Set myConn=Server.CreateObject ("ADODB.Connection")
Myconn. ConnectionString=ConnStr
myConn.open  
%>
连接access数据库
<%
Dim ConnStr,myConn,mySQL
ConnStr="Provider=microsoft.jet.oledb.4.0;data source=" & server.mappath ("../Upload/Upload.mdb") 
Set myConn=Server.CreateObject ("ADODB.Connection")
Myconn. ConnectionString=ConnStr
myConn.open  
%>

Connection对象除了连接数据库之外,也能够操作数据库,如直接执行sql语句的插入,删除,更新及检索数据。
如:
<%
Dim ConnStr,myConn,mySQL
ConnStr="Provider=SQLOLEDB;data source=(local);initial catalog=pubs;user id=sa;password=;"  
Set myConn=Server.CreateObject ("ADODB.Connection")
Myconn. ConnectionString=ConnStr
myConn.open  
mySQL="update authors set au_lname='black' where au_id='172-32-1176'"
myConn.execute(mySQL)
response.write "记录更新成功,au_lname=white的被改成black"
%>

RECORDSET对象
 Activeconnection属性  指示指定的recordset对象所属的connection对象
 Absolutepage属性  指示从1到recordset对象所含页数的长整形值,或者常量;adposunknown,recordset为空,当前位置未知,或者提供者不支持absolutepage属性;adposbof,当前记录指针位于bof:adposeof,当前指针位于eof。
 Absoluteposition属性 指示从1到recordset对象所含页数的长整形值,或者指示与absolutepage属性相同常量
 Bof,eof,属性 当前记录在recordset对象的第一个位置或最后一个位置
 Cursorlocation属性 游标服务的位置,某个常量的长整形值。
 Cursortype属性 指示recordset对象使用的游标类型。
 Pagecount属性 只读,指示recordset对象包含的数据页数
 Pagesize属性  指示recordset中一页所包含的记录数。默认为10
 Recordsetcount属性 只读,指示recordset对象中的记录的当前数目
 State属性 只读,布儿型,说明recordset对象当前状态是连接,执行或是获取。
 Addnew方法  添加新记录
 Delete方法  删除当前记录
 Move方法  移动recordset对象当前记录的位置,recordset.move n,start   n表示要移动的记录数,负数表示向后移动,start表示根据游标中的bookmark移动记录指针,如果不传送这个变量则移动是相对当前记录而言的。当然如果recordset不支持书签,则不能使用。
 Movefirst,movelast,moveprevious,movenext方法  移动记录
 Open方法 打开游标。
 Updata方法 保存对recordset对象的当前记录所做的所有更改
 Fields集合  包含recordset对象的所有的field对象
 Properties集合 包含recordset对象的所有的property对象
 Getrows方法  可以用来从数据源中取数据,并把它放入一个数组中,因此如果想从数据源中取出大量的数据,则这种方法很有用。因为通常数组消耗的资源比recordset小,可以通过下句语法:myarray=recordset.getrows(rows,start,fields),其中三个参数,rows参数将返回数组的行数。如果该变量没有值,那么recordset中所剩下的行都将被取回到数组中。如果把一个书签值传给方法的start变量,那么将从书签开始取出数据放入数组。Fields是定义要取数据的字段。
 Cancelupdate方法  取消在调用update方法前所做的一些修改。


具体的例题:
1.通过recordset对象移动数据。
<%
Dim ConnStr,myConn,mySQL,myRec
ConnStr="Provider=SQLOLEDB;data source=(local);initial catalog=pubs; user id=sa;password=;"  
set myRec=Server.CreateObject ("ADODB.RecordSet")
myRec.open "select * from authors",ConnStr,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>" 
'移动到最后1条记录
 myRec.movelast
'输出最后1条记录
 response.write "<tr><td>最后1条</td><td>" & myRec("au_fname") & "</td><td>" & myRec("phone") & "</td><td>" & myRec("address") & "</td></tr>"
'移动到倒数第2条记录,通过MovePrevious方法
'移动后要查看是否到了记录的最前头,因为有可能只有一条记录
'所以要用Bof判断
 myRec.MovePrevious
 if not myRec.Bof then
'输出倒数第2条记录
  response.write "<tr><td>倒数第2条</td><td>" & myRec("au_fname") & "</td><td>" & myRec("phone") & "</td><td>" & myRec("address") & "</td></tr>"
 end if
'第1条记录
 myRec.moveFirst
'输出第1条记录
 response.write "<tr><td>第1条</td><td>" & myRec("au_fname") & "</td><td>" & myRec("phone") & "</td><td>" & myRec("address") & "</td></tr>"
'移到下一条
 myRec.movenext
'移动到下一条后,要判断是否到了记录的最后头
 if not myRec.Eof then
'输出第2条记录
  response.write "<tr><td>第2条</td><td>" & myRec("au_fname") & "</td><td>" & myRec("phone") & "</td><td>" & myRec("address") & "</td></tr>"
 end if
 myBookMark=myRec.bookmark   '将第2条记录保存为书签
'输出第10条
'通过Move方法移动到第10条,最好在移动之前判断总体记录数
'判断记录数量可以通过RecordCout方法,在后面例题中会介绍
'注意,move后面的第1个参数是移动的数量,第2个参数是开始移动的位置
'如果不传送第2个参数,则表示的是从当前记录开始移动
'如果要传送,则只能传送书签BookMark
 myRec.move 10,myBookMark  '前面已经保存了书签,因此是从第2条向后移动10条
'输出后移10条的记录
 response.write "<tr><td>后移10条</td><td>" & myRec("au_fname") & "</td><td>" & myRec("phone") & "</td><td>" & myRec("address") & "</td></tr>"
'此时记录已经后移了10条 ,但是如果想快速地回到刚才的第2条,则可以通过使用书签
'因为刚才已经保存第2条为书签
 myRec.bookmark=myBookMark
'输出书签保存的记录
 response.write "<tr><td>书签记录</td><td>" & myRec("au_fname") & "</td><td>" & myRec("phone") & "</td><td>" & myRec("address") & "</td></tr>"
 response.write "</table>" 
else
 response.write "表格中没有记录!"
end if
myRec.close
set myRec=nothing
%>

2.通过recordcount方法统计记录数量
<%
Dim ConnStr,myConn,mySQL,myRec
ConnStr="Provider=SQLOLEDB;data source=(local);initial catalog=pubs; user id=sa;password=;"  
set myRec=Server.CreateObject ("ADODB.RecordSet")
myRec.ActiveConnection=ConnStr
myRec.open "select * from authors",,1,1  '打开记录集
response.write "通过RecordCount属性统计的记录数为:" & myRec.RecordCount
myRec.close  '关闭记录集
myRec.open "select count(*) as total from authors",,1,1  '打开记录集
response.write "<br>通过SQL统计的记录数为:" & myRec("total")
myRec.close
set myRec=nothing
%>


3.分页显示记录
<%
Dim ConnStr,myConn,mySQL,myRec,myPage,myPageSize
'首先获得用户的显示参数
'获得要显示的页数
myPage=request.querystring("whichpage")  '获得须显示的页数
If  myPage="" then    '如果为空,则显示第一页
   myPage=1
end if
'获得每页的大小,即显示记录的条数
myPageSize=request.querystring("pagesize")    '获得每页大小
'如果为空
If  myPageSize="" then
   myPageSize=5         '如果每页大小为空,则默认为5条记录
end if
ConnStr="Provider=SQLOLEDB;data source=(local);initial catalog=pubs; user id=sa;password=;"  
set myRec=Server.CreateObject ("ADODB.RecordSet")
myRec.ActiveConnection=ConnStr
'设定RecordSet的属性
'myRec.cursorlocation=aduseclient
myRec.cachesize=5
myRec.open "select * from authors",,1,3  '打开记录集
'设定分页设置
myRec.movefirst  '移动到第1条记录
' 设定每页的大小,显示记录的条数
myRec.pagesize=mypagesize
'总页数
maxcount=cint(myRec.pagecount)
'设定要显示的是第几页
myRec.absolutepage=myPage
'用户记录每页显示的记录数
howmanyrecs=5
response.write "当前页:" & mypage & "   总页数:" & maxcount & "<br>"
response.write "<table border=1>"
'首先输出字段标题,用表格来表示
response.write "<tr><td>作者编号</td><td>电话</td><td>地址</td></tr>" 
do while not myRec.eof  and  cint(howmanyrec)<cint(mypagesize)   '表示当前记录不是最后
     '输出一条记录
     response.write "<tr><td>" & myRec("au_id") & "</td><td>" &   myRec("phone") & "</td><td>" & myRec("address") & "</td></tr>"
     '接着移动到下一条记录
     myRec.movenext  '注意一定要使用这个语句,否则会死循环
     howmanyrec=howmanyrec+1
loop
response.write "</table>"
'接着显示其他的链接,用户导航到其他页
'首先获得当前网页的路径
scriptname=request.servervariables("script_name")
'循环显示所有的连接
for counter=1 to maxcount
   If counter>=10 then
      pad=""
   end if
   ref="<a href='" & scriptname & "?whichpage=" & counter
   ref=ref & "&pagesize=" & mypagesize & "'>" & pad & counter & "</a>"
   response.write ref & " "
   if counter mod 10 = 0 then
      response.write "<br>"
   end if
next
myRec.close
set myRec=nothing
%>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics