从Server.Execute生成的文件访问连接字符串

时间:2014-04-11 20:18:04

标签: vbscript asp-classic filesystemobject

我有一个文件header.asp,其服务器端包含head.asp。在head.asp内部,我创建了与数据库的连接,可以从header.asp文件和使用header.asp文件作为服务器端包含的页面访问(在这种情况下,我的core.asp文件)。在core.asp内,我有以下内容:

if request.querystring("page") = "" then
  response.write("<p>No data to load</p>")
else
  page = request.querystring("page")
  set fso = createobject("scripting.filesystemobject")
  FileName = "../Pages/" & page & ".asp"
  if fso.FileExists (server.mappath(FileName)) then 
    Server.Execute(FileName)
  else
    response.write("<p>Could not validate page. Try again.</p>")
  end if
end if

执行得很好,并在core.asp文件中呈现正确的页面。我的问题是,连接字符串(来自head.asp)对于从Server.Execute调用的文件不可用。因此,除非我重新实例化对象,否则我无法在此页面上运行数据库查询等。有没有办法使用创建的对象?

如果我没有正确解释,我将阐述,因为我能够给出我的中级经验水平。

2 个答案:

答案 0 :(得分:0)

将此对象分配给会话变量,您可以从任何您喜欢的页面访问它......

Session("MyConnectionObj")= CONN

CONN是您最初实例化连接的页面上的实例化连接对象。在你执行的文件中只需调用

 set NewCONN=Session("MyConnectionObj")
 RS.Open strSql ,NewCONN,1,1

其中strSql是你的SQL,RS是你的记录集等等......显然,如果你调用存储过程,它看起来会略有不同,但你明白了。

编辑显示必须在使用它之前声明和设置对象。至少我看到一个&#34;专家&#34;我不知道。

根据Window Frog的评论编辑:

  你是否尝试过这样做:

<%
if request.querystring("page") = "" then
  response.write("<p>No data to load</p>")
else
  page = request.querystring("page")
  set fso = createobject("scripting.filesystemobject")
  FileName = "../Pages/" & page & ".asp"
  if fso.FileExists (server.mappath(FileName)) then%> 
   <!--#include file="<%=FileName %>" -->
 <% else
    response.write("<p>Could not validate page. Try again.</p>")
  end if
end if
%>

答案 1 :(得分:0)

作为对#1的回答,如果我正确地遵循逻辑,我正在尝试在应用程序级别创建连接对象(global.asa):     

Sub Application_OnStart() 
dim oConn, connectstr
set oConn = Server.CreateObject("ADODB.Connection") 
connectstr = "Driver={MySQL ODBC 3.51 Driver};SERVER=theServer; DATABASE=theDatabase; UID=theUserID; PWD=thePassword"
oConn.ConnectionTimeout = 5
oConn.Open connectstr
Application("con") = oConn
end Sub

</script>

尝试从包含global.asa作为IIS的页面访问这样的对象:     qryAdmin =“select * from table”     set rsAdmin = Application(“con”)。execute(qryAdmin)

结果是一样的。我收到“需要对象”错误。任何可能引起这种情况的明显错误?

相关问题