日期:2010-02-15  浏览次数:20810 次

以下为引用的内容:
<%
Dim oConn,oRs
Set oConn = Server.CreateObject("ADODB.Connection")
Set ors = Server.CreateObject("ADODB.RecordSet")
oConn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("db1.mdb")
Session("conn") = oConn
ors.open "select * from t1",Session("conn"),1,1
    Response.write(oRs(0))
ors.close
Set ors = Nothing
oConn.close
Set oConn = Nothing
%>

这是网上流传的把Connection对象保存在Seesion对象中的代码,注意其中的:Session("conn") = oConn,对象的复制就是这样子的吗?下边我们来验证一下:

以下为引用的内容:
<%
Dim oConn,oRs
Set oConn = Server.CreateObject("ADODB.Connection")
Set ors = Server.CreateObject("ADODB.RecordSet")
oConn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("db1.mdb")
Session("conn") = oConn
ors.open "select * from t1",Session("conn"),1,1
    Response.write(Session("conn").ConnectionString)
ors.close
Set ors = Nothing
oConn.close
Set oConn = Nothing
%>

结果出错,提示"缺少对象: 'Session(...)'"!这说明对象并没有复制成功,那Session("conn")是什么类型的呢?我们用TypeName(Session("conn"))来测试下,结果为"String"!出于好奇,我们直接输出Session("conn")看看:

以下为引用的内容:
<%
Dim oConn,oRs
Set oConn = Server.CreateObject("ADODB.Connection")
Set ors = Server.CreateObject("ADODB.RecordSet")
oConn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("db1.mdb")
Session("conn") = oConn
ors.open "select * from t1",Session("conn"),1,1
    Response.write(Session("conn"))
ors.close
Set ors = Nothing
oConn.close
Set oConn = Nothing
%>

结果为:

以下为引用的内容:
Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=E:\www\db1.mdb;Mode=Share Deny None;
Extended Properties=&