可变范围和功能 [英] Variable Scope and Functions

查看:46
本文介绍了可变范围和功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,


我正在尝试做一些应该非常简单的事情,但我想我已经支持了b $ b太多小时或者其他什么。


基本上,我有一个使用数据库连接的asp应用程序,

我想声明(创建)该连接包含并使用它在整个应用程序中使用各种函数
。我已经尝试将

连接对象设为公共变量,常量,使函数

public,在byRef,byVal等中传递连接对象...我只是

无法让它工作。这在经典的ASP中是不可能的吗?


我还应该提一下,如果我从这个函数中取出这个代码,那么它将按预期运行.b $ b按预期工作。


谢谢!


Teg Ryan


以下是我要做的一个示例:


包括:


<%


public cn

public rs

public sql

set cn = Server.CreateObject(" ADODB.Connection")

set rs = Server.CreateObject(" ADODB.RecordSet")

cn.Open" Provider = MSDASQL; Driver = {Microsoft Access Driver(* .mdb)};

DBQ = [[path]];"


%>


然后这里是ASP文件中的一个函数,其顶部包含




函数SQLSelect()


sql =" select * from TABLE"

rs.open sql,cn,3,3

......等等


结束功能

Hey All,

I''m trying to do something that should be very simple, but I think I''ve
been up for too many hours or something.

Basically, I have an asp application that uses a database connection,
and I want to declare (create) that connection in an include and use it
throughout the application in various functions. I''ve tried making the
connection object a public variable, a constant, making the functions
public, passing the connection object in byRef, byVal, etc... I just
can''t get it to work. Is this not possible in classic ASP?

I should also mention that if I take this code out of the function it
works as expected.

Thanks!

Teg Ryan

Here is a sample of what i''m trying to do:

Include:

<%

public cn
public rs
public sql

set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
DBQ=[[path]];"

%>

Then here''s one of the functions in the ASP file that has the include
at the top:

Function SQLSelect()

sql = "select * from TABLE"
rs.open sql,cn,3,3
... etc

End function

推荐答案

te ***** @ gmail.com 写道:

Hey All,


我正在尝试做一些应该非常简单的事情,但我认为

我已经花了太多时间或其他什么。


基本上,我有一个使用数据库连接的asp应用程序,

我想在include中声明(创建)该连接并使用

它在整个应用程序中的各种功能。我已经尝试了

使连接对象成为一个公共变量,一个常量,使公共函数变为
,在byRef中传递连接对象,byVal,

等......我无法让它工作。这是不可能的经典

ASP?


我还应该提一下,如果我把这个代码从功能中取出它

按预期工作。


谢谢!


Teg Ryan


这是一个样本我想做什么:


包括:


<%


public cn

public rs

public sql


set rs = Server.CreateObject(" ADODB.RecordSet")

cn.Open" Provider = MSDASQL; Driver = {Microsoft Access Driver(* .mdb)};

DBQ = [[path]];"
Hey All,

I''m trying to do something that should be very simple, but I think
I''ve been up for too many hours or something.

Basically, I have an asp application that uses a database connection,
and I want to declare (create) that connection in an include and use
it throughout the application in various functions. I''ve tried
making the connection object a public variable, a constant, making
the functions public, passing the connection object in byRef, byVal,
etc... I just can''t get it to work. Is this not possible in classic
ASP?

I should also mention that if I take this code out of the function it
works as expected.

Thanks!

Teg Ryan

Here is a sample of what i''m trying to do:

Include:

<%

public cn
public rs
public sql

set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
DBQ=[[path]];"



与您的问题无关,但是:
http://www.aspfaq.com/show.asp?id=2126


>

%>


然后这里是ASP文件中包含

的函数之一在顶部:


函数SQLSelect()

sql =" select * from TABLE"

rs .open sql,cn,3,3

等等


结束函数
>
%>

Then here''s one of the functions in the ASP file that has the include
at the top:

Function SQLSelect()

sql = "select * from TABLE"
rs.open sql,cn,3,3
... etc

End function



这应该有效。尝试时会发生什么?


-

Microsoft MVP - ASP / ASP.NET

请回复新闻组。我的From

标题中列出的电子邮件帐户是我的垃圾邮件陷阱,因此我不经常检查它。通过发布到新闻组,您将获得更快的回复。

This should work. What happens when you try it?

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don''t check it very often. You will get a
quicker response by posting to the newsgroup.




嘿鲍勃,谢谢快速回复!


首先,我以为我使用的是原生的OLEDB提供商....哈哈!我在做什么?b $ b我在干嘛?我还没有为那个盒子创建一个DSN,它看起来不像我以前用过的DSNless连接那样...... b / b $

其次,更重要的是,我忘记了错误

我得到的消息:


错误类型:

Microsoft VBScript运行时(0x800A01A8)

所需对象:''rs''


我现在要仔细检查,但我相信如果我复制

连接代码进入有问题的功能,它工作正常。


谢谢!


Teg


Bob Barrows [MVP]写道:

Hey Bob, thanks for the quick reply!

Firstly, I thought I was using a native OLEDB provider....lol! What am
I doing? I haven''t created a DSN for that box, and it doesn''t look
like the DSNless connections I''m used to...

Secondly and more importantantly, I forgot to leave out the error
message I am getting:

Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: ''rs''

I am going to double check right now, but I believe that if I copy the
connection code into the function in question, it works fine.

Thanks!

Teg

Bob Barrows [MVP] wrote:
te * **** @gmail.com 写道:

嘿所有,


我正在尝试做一些应该非常简单的事情,但我认为

我已经上了太多时间了。


基本上,我有一个asp应用你建立一个数据库连接,

我想在一个include中声明(创建)该连接,并在各种函数的整个应用程序中使用

。我已经尝试了

使连接对象成为一个公共变量,一个常量,使公共函数变为
,在byRef中传递连接对象,byVal,

等......我无法让它工作。这是不可能的经典

ASP?


我还应该提一下,如果我把这个代码从功能中取出它

按预期工作。


谢谢!


Teg Ryan


这是一个样本我想做什么:


包括:


<%


public cn

public rs

public sql


set rs = Server.CreateObject(" ADODB.RecordSet")

cn.Open" Provider = MSDASQL; Driver = {Microsoft Access Driver(* .mdb)};

DBQ = [[path]];"
Hey All,

I''m trying to do something that should be very simple, but I think
I''ve been up for too many hours or something.

Basically, I have an asp application that uses a database connection,
and I want to declare (create) that connection in an include and use
it throughout the application in various functions. I''ve tried
making the connection object a public variable, a constant, making
the functions public, passing the connection object in byRef, byVal,
etc... I just can''t get it to work. Is this not possible in classic
ASP?

I should also mention that if I take this code out of the function it
works as expected.

Thanks!

Teg Ryan

Here is a sample of what i''m trying to do:

Include:

<%

public cn
public rs
public sql

set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
DBQ=[[path]];"



与您的问题无关,但是:
http://www.aspfaq.com/show.asp?id=2126



%>


然后这里是ASP文件中的一个函数,其顶部包含




函数SQLSelect()

sql =" select * from TABLE"

rs.open sql,cn, 3,3

...等


结束功能

%>

Then here''s one of the functions in the ASP file that has the include
at the top:

Function SQLSelect()

sql = "select * from TABLE"
rs.open sql,cn,3,3
... etc

End function



这应该有效。尝试时会发生什么?


-

Microsoft MVP - ASP / ASP.NET

请回复新闻组。我的From

标题中列出的电子邮件帐户是我的垃圾邮件陷阱,因此我不经常检查它。通过发布到新闻组,您将获得更快的回复


This should work. What happens when you try it?

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don''t check it very often. You will get a
quicker response by posting to the newsgroup.




是的,当我把连接信息放在

函数中时工作得很好。


另外,我应该提到我正在使用Access,而不是SQL Server。不确定

如果重要...


tegr ... @ gmail.com写道:

yup, works just fine when I put the connection information in the
function.

Also, I should mention that I''m using Access, not SQL Server. Not sure
if that matters...

tegr...@gmail.com wrote:

嘿鲍勃,谢谢快速回复!


首先,我以为我使用的是原生OLEDB提供商....哈哈!我在做什么?b $ b我在干嘛?我还没有为那个盒子创建一个DSN,它看起来不像我以前用过的DSNless连接那样...... b / b $

其次,更重要的是,我忘记了错误

我得到的消息:


错误类型:

Microsoft VBScript运行时(0x800A01A8)

所需对象:''rs''


我现在要仔细检查,但我相信如果我复制

连接代码进入有问题的功能,它工作正常。


谢谢!


Teg


Bob Barrows [MVP]写道:
Hey Bob, thanks for the quick reply!

Firstly, I thought I was using a native OLEDB provider....lol! What am
I doing? I haven''t created a DSN for that box, and it doesn''t look
like the DSNless connections I''m used to...

Secondly and more importantantly, I forgot to leave out the error
message I am getting:

Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: ''rs''

I am going to double check right now, but I believe that if I copy the
connection code into the function in question, it works fine.

Thanks!

Teg

Bob Barrows [MVP] wrote:
te * **** @gmail.com 写道:

嘿所有,

>

我正在尝试做一些非常简单的事情,但我认为

我已经上了太多时间了。

>

基本上,我有一个asp appl使用数据库连接,

,我想在include中声明(创建)该连接,并在各种函数的整个应用程序中使用

。我已经尝试了

使连接对象成为一个公共变量,一个常量,使公共函数变为
,在byRef中传递连接对象,byVal,

等......我无法让它工作。这是不可能的经典

ASP?

>

我还应该提一下,如果我从函数中取出这个代码吗

按预期工作。

>

谢谢!

>

Teg Ryan

>

以下是我要做的一个示例:

>

包括:

>

<%

>

public cn

public rs

public sql

>

set cn = Server.CreateObject(" ADODB.Connection")

set rs = Server.CreateObject(" ADODB.RecordSet")

cn.Open" Provider = MSDASQL; Driver = {Microsoft Access Driver(* .mdb)};

DBQ = [[path]];"
Hey All,
>
I''m trying to do something that should be very simple, but I think
I''ve been up for too many hours or something.
>
Basically, I have an asp application that uses a database connection,
and I want to declare (create) that connection in an include and use
it throughout the application in various functions. I''ve tried
making the connection object a public variable, a constant, making
the functions public, passing the connection object in byRef, byVal,
etc... I just can''t get it to work. Is this not possible in classic
ASP?
>
I should also mention that if I take this code out of the function it
works as expected.
>
Thanks!
>
Teg Ryan
>
Here is a sample of what i''m trying to do:
>
Include:
>
<%
>
public cn
public rs
public sql
>
set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
DBQ=[[path]];"



与您的问题无关,但是:
http://www.aspfaq.com/show.asp?id=2126


>

%>

>

然后这里是ASP文件中包含include
在顶部:

>

函数SQLSelect()

>

sql =" select * from TABLE"

rs.open sql,cn,3,3

... etc

>

结束函数
>
%>
>
Then here''s one of the functions in the ASP file that has the include
at the top:
>
Function SQLSelect()
>
sql = "select * from TABLE"
rs.open sql,cn,3,3
... etc
>
End function



这应该有效。尝试时会发生什么?


-

Microsoft MVP - ASP / ASP.NET

请回复新闻组。我的From

标题中列出的电子邮件帐户是我的垃圾邮件陷阱,因此我不经常检查它。通过发布到新闻组,您将获得更快的回复

This should work. What happens when you try it?

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don''t check it very often. You will get a
quicker response by posting to the newsgroup.


这篇关于可变范围和功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆