断开的数据库连接的一个例子

使用断开的Recordset的一个关键条件就是使用客户端临时表,即

指定Rs.CursorLocation = adUseClient or 3,下面是一个完整
的例子:
demo-1.asp (在线演示:http://www.aspcn.com/demo/demo-1.asp)
------------------------------------------------------------

1@ Language=VBScript 
 1   
 2'# -------------------------------------------------------------------   
 3\---------   
 4'# 程序描述:演示使用断开的记录集   
 5'# 程序设计:亚豪   
 6'# -------------------------------------------------------------------   
 7\---------   
 8  
 9On Error Resume Next   
10Dim adoConn,adoRs,SQLCmd,ConnectString   
11'#--------------------------------------------------------------------   
12\---------   
13'# 使用SQL Server的 DSN-less 方式连接数据库   
14'#--------------------------------------------------------------------   
15\---------   
16ConnectString = "Driver={SQL Server};" & _   
17"Server=(local);" & _   
18"Database=abc;" & _   
19"Uid=sa;" & _   
20"Pwd=123"   
21  
22'#--------------------------------------------------------------------   
23\---------   
24'# 创建对象实例,并初始化连接(Connection)   
25'#--------------------------------------------------------------------   
26\---------   
27Set adoConn = Server.CreateObject("ADODB.Connection")   
28Set adoRs = Server.CreateObject("ADODB.Recordset")   
29adoConn.Open ConnectString   
30  
31SQLCmd = "Select * from bbs_user where LTrim(RTrim(id)) = 'w3org'"   
32'#--------------------------------------------------------------------   
33\---------   
34'# 使用客户端临时表打开并保存记录集,关键所在!   
35'# 客户端是运行ASP程序的主机,相对于数据库服务器而言   
36'#--------------------------------------------------------------------   
37\---------   
38adoRs.CursorLocation = 3 'Const adUseClient = 3   
39adoRs.Open SQLCmd,adoConn,0,2   
40  
41'#--------------------------------------------------------------------   
42\---------   
43'# 没有发现记录,结束程序   
44'#--------------------------------------------------------------------   
45\---------   
46Err.Clear   
47If adoRs.EOF And adoRs.BOF Then   
48adoRs.Close   
49adoConn.Close   
50Set adoRs = Nothing   
51Set adoConn = Nothing   
52Response.Write "用户没有发现!"   
53Response.End   
54End If   
55'#--------------------------------------------------------------------   
56\---------   
57'# 找到记录,显示原来的值后释放记录集的活动连接,之后关闭数据库连接   
58'#--------------------------------------------------------------------   
59\---------   
60Response.Write "原来的昵称是:" & Trim(adoRs.Fields("NickName").Value)   
61& "

<br/>

1" & vbCrLf   
2Set adoRs.ActiveConnection = Nothing '释放Rs的活动连接   
3adoConn.Close   
4Response.Write "连接已经关闭!

<br/>

 1" & vbCrLf   
 2'#--------------------------------------------------------------------   
 3\---------   
 4'# 赋一个新的值给记录的 NickName 列,这时已经没有活动的数据库连接   
 5'#--------------------------------------------------------------------   
 6\---------   
 7adoRs.Fields("NickName").Value = "NewTest-" & Second(Time())   
 8'#--------------------------------------------------------------------   
 9\---------   
10'# 再次打开数据库连接,并绑定到先前的记录集上   
11'#--------------------------------------------------------------------   
12\---------   
13adoConn.Open ConnectString   
14adoRs.ActiveConnection = adoConn   
15'#--------------------------------------------------------------------   
16\---------   
17'# 更新记录集,并显示新的值   
18'#--------------------------------------------------------------------   
19\---------   
20adoRs.Update   
21Response.Write "新的昵称是:" & Trim(adoRs.Fields("NickName").Value) &   
22"

<br/>

1" & vbCrLf '显示一个结果   
2'#--------------------------------------------------------------------   
3\---------   
4'# 如果发生错误则显示错误   
5'#--------------------------------------------------------------------   
6\---------   
7If Err.number <> 0 Then   
8Response.Write "发生错误:" & Err.description & "

<br/>

1"   
2End If   
3adoRs.Close   
4adoConn.Close   
5Set adoRs = Nothing   
6Set adoConn = Nothing   
1<html>
2<head><title>演示断开的连接</title></head></html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus