| Gzu521.com我的学习网 |
|
我们在使用asp 内置的ado组件进行数据库编程时,通常是在脚本的开头打开一个连接,并在脚本的最后关闭它,但是就较大脚本而言,在多数情况下连接打开的时间要比它需要打开的时间长得多。因此为了节省服务器资源,应该尽可能关闭连接以释放连接所占有的资源,这种关闭记录集的连接而不关闭记录集的技术叫做断开记录集,这个记录集本身则称为断开的记录集。 下面我们就通过一个实例来说明这种技术的使用方法(northwind.mdb是microsoft access97自带的一个数据库,文件adovbs.inc可在c:\program files\common files\system\ado下找到): %\]CK9G=v!# )n [ 本 资 料 来 源 于 贵 州 学 习 网 网络编程ASP教程 http://Www.gzU521.com ] %\]CK9G=v!# )n <% @language= vbscript %> <!--#includefile="adovbs.inc"--> <% response.expires = 0 dim cnn,objrs, strout, strq, strc strc= "driver={microsoft access driver (*.mdb)}; dbq=" & server.mappath("\asp24") & "\northwind.mdb;" '建立连接 set cnn = server.createobject("adodb.connection") cnn.open strc '创建recordset对象 set objrs = server.createobject("adodb.recordset") objrs.cursorlocation =aduseclient objrs.cursortype = adopenstatic objrs.locktype = adlockoptimistic strq = "select 运货商id, 公司名称, 电话 from 运货商 " objrs.open strq, cnn, , , adcmdtext set objrs.activeconnection = nothing '断开记录集 cnn.close '关闭连接 set cnn = nothing *n9.K [(*h;3|[ 此文转贴于我的学习网网络编程ASP教程 http://www.Gzu521.com]*n9.K [(*h;3| response.write "<html><body>" '下面使用断开的记录集 do while (notobjrs.eof) strout = objrs("运货商id") & ", " & objrs("公司名称") & ", " & objrs("电话") response.write server.htmlencode(strout) & "<br>" objrs.movenext loop response.write "<br>准备新增或插入记录: " '若需要更新数据库, 则要重新建立连接 set cnn = server.createobject("adodb.connection") cnn.open strc set objrs.activeconnection = cnn objrs.filter = "公司名称 = '吴丰'" if objrs.eof then objrs.addnew objrs("公司名称") = "吴丰" objrs("电话") = "571-7227298" objrs.update response.write "符合该条件的记录不存在, 则新增.<br>" else objrs("电话") = "571-7227071" response.write "符合该条件的记录存在, 则 update.<br>" objrs.update end if set objrs.activeconnection = nothing cnn.close set cnn = nothing objrs.close set objrs = nothing response.write "</body></html>" oSPk:aJ2hri}+[7d[本_文_来_源_于_我_的_学_习_网网络编程ASP教程 http://Www.GZU521.Com ]oSPk:aJ2hri}+[7d %> |
责任编辑:gzu521