使用断开的Recordset的一个关键条件就是使用客户端临时表,即
指定Rs.CursorLocation = adUseClient or 3,下面是一个完整
的例子:
demo-1.asp (在线演示:http://www.aspcn.com/demo/demo-1.asp)
------------------------------------------------------------
<%@ Language=VBScript %>
<%
'# -------------------------------------------------------------------
---------
'# 程序描述:演示使用断开的记录集
'# 程序设计:亚豪
'# -------------------------------------------------------------------
---------
On Error Resume Next
Dim adoConn,adoRs,SQLCmd,ConnectString
'#--------------------------------------------------------------------
---------
'# 使用SQL Server的 DSN-less 方式连接数据库
'#--------------------------------------------------------------------
---------
ConnectString = "Driver={SQL Server};" & _
"Server=(local);" & _
"Database=abc;" & _
"Uid=sa;" & _
"Pwd=123"
'#--------------------------------------------------------------------
---------
'# 创建对象实例,并初始化连接(Connection)
'#--------------------------------------------------------------------
---------
Set adoConn = Server.CreateObject("ADODB.Connection")
Set adoRs = Server.CreateObject("ADODB.Recordset")
adoConn.Open ConnectString
SQLCmd = "Select * from bbs_user where LTrim(RTrim(id)) = 'w3org'"
'#--------------------------------------------------------------------
---------
'# 使用客户端临时表打开并保存记录集,关键所在!
'# 客户端是运行ASP程序的主机,相对于数据库服务器而言
'#--------------------------------------------------------------------
---------
adoRs.CursorLocation = 3 'Const adUseClient = 3
adoRs.Open SQLCmd,adoConn,0,2
'#--------------------------------------------------------------------
---------
'# 没有发现记录,结束程序
'#--------------------------------------------------------------------
---------
Err.Clear
If adoRs.EOF And adoRs.BOF Then
adoRs.Close
adoConn.Close
Set adoRs = Nothing
Set adoConn = Nothing
Response.Write "用户没有发现!"
Response.End
End If
'#--------------------------------------------------------------------
---------
'# 找到记录,显示原来的值后释放记录集的活动连接,之后关闭数据库连接
'#--------------------------------------------------------------------
---------
Response.Write "原来的昵称是:" & Trim(adoRs.Fields("NickName").Value)
& "<br>" & vbCrLf
Set adoRs.ActiveConnection = Nothing '释放Rs的活动连接
adoConn.Close
Response.Write "连接已经关闭!<br>" & vbCrLf
'#--------------------------------------------------------------------
---------
'# 赋一个新的值给记录的 NickName 列,这时已经没有活动的数据库连接
'#--------------------------------------------------------------------
---------
adoRs.Fields("NickName").Value = "NewTest-" & Second(Time())
'#--------------------------------------------------------------------
---------
'# 再次打开数据库连接,并绑定到先前的记录集上
'#--------------------------------------------------------------------
---------
adoConn.Open ConnectString
adoRs.ActiveConnection = adoConn
'#--------------------------------------------------------------------
---------
'# 更新记录集,并显示新的值
'#--------------------------------------------------------------------
---------
adoRs.Update
Response.Write "新的昵称是:" & Trim(adoRs.Fields("NickName").Value) &
"<br>" & vbCrLf '显示一个结果
'#--------------------------------------------------------------------
---------
'# 如果发生错误则显示错误
'#--------------------------------------------------------------------
---------
If Err.number <> 0 Then
Response.Write "发生错误:" & Err.description & "<br>"
End If
adoRs.Close
adoConn.Close
Set adoRs = Nothing
Set adoConn = Nothing
%>
<HTML>
<HEAD><Title>演示断开的连接</title></HEAD>
指定Rs.CursorLocation = adUseClient or 3,下面是一个完整
的例子:
demo-1.asp (在线演示:http://www.aspcn.com/demo/demo-1.asp)
------------------------------------------------------------
<%@ Language=VBScript %>
<%
'# -------------------------------------------------------------------
---------
'# 程序描述:演示使用断开的记录集
'# 程序设计:亚豪
'# -------------------------------------------------------------------
---------
On Error Resume Next
Dim adoConn,adoRs,SQLCmd,ConnectString
'#--------------------------------------------------------------------
---------
'# 使用SQL Server的 DSN-less 方式连接数据库
'#--------------------------------------------------------------------
---------
ConnectString = "Driver={SQL Server};" & _
"Server=(local);" & _
"Database=abc;" & _
"Uid=sa;" & _
"Pwd=123"
'#--------------------------------------------------------------------
---------
'# 创建对象实例,并初始化连接(Connection)
'#--------------------------------------------------------------------
---------
Set adoConn = Server.CreateObject("ADODB.Connection")
Set adoRs = Server.CreateObject("ADODB.Recordset")
adoConn.Open ConnectString
SQLCmd = "Select * from bbs_user where LTrim(RTrim(id)) = 'w3org'"
'#--------------------------------------------------------------------
---------
'# 使用客户端临时表打开并保存记录集,关键所在!
'# 客户端是运行ASP程序的主机,相对于数据库服务器而言
'#--------------------------------------------------------------------
---------
adoRs.CursorLocation = 3 'Const adUseClient = 3
adoRs.Open SQLCmd,adoConn,0,2
'#--------------------------------------------------------------------
---------
'# 没有发现记录,结束程序
'#--------------------------------------------------------------------
---------
Err.Clear
If adoRs.EOF And adoRs.BOF Then
adoRs.Close
adoConn.Close
Set adoRs = Nothing
Set adoConn = Nothing
Response.Write "用户没有发现!"
Response.End
End If
'#--------------------------------------------------------------------
---------
'# 找到记录,显示原来的值后释放记录集的活动连接,之后关闭数据库连接
'#--------------------------------------------------------------------
---------
Response.Write "原来的昵称是:" & Trim(adoRs.Fields("NickName").Value)
& "<br>" & vbCrLf
Set adoRs.ActiveConnection = Nothing '释放Rs的活动连接
adoConn.Close
Response.Write "连接已经关闭!<br>" & vbCrLf
'#--------------------------------------------------------------------
---------
'# 赋一个新的值给记录的 NickName 列,这时已经没有活动的数据库连接
'#--------------------------------------------------------------------
---------
adoRs.Fields("NickName").Value = "NewTest-" & Second(Time())
'#--------------------------------------------------------------------
---------
'# 再次打开数据库连接,并绑定到先前的记录集上
'#--------------------------------------------------------------------
---------
adoConn.Open ConnectString
adoRs.ActiveConnection = adoConn
'#--------------------------------------------------------------------
---------
'# 更新记录集,并显示新的值
'#--------------------------------------------------------------------
---------
adoRs.Update
Response.Write "新的昵称是:" & Trim(adoRs.Fields("NickName").Value) &
"<br>" & vbCrLf '显示一个结果
'#--------------------------------------------------------------------
---------
'# 如果发生错误则显示错误
'#--------------------------------------------------------------------
---------
If Err.number <> 0 Then
Response.Write "发生错误:" & Err.description & "<br>"
End If
adoRs.Close
adoConn.Close
Set adoRs = Nothing
Set adoConn = Nothing
%>
<HTML>
<HEAD><Title>演示断开的连接</title></HEAD>