可变范围(ASP.Net) [英] Variable Scope (ASP.Net)

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

问题描述

如果我为页面的每个

请求声明一个模块级别的oledbconnection,那么该变量仍在范围内?我在

页面的后续调用中得到

open.executing错误消息。


我认为ASP.Net是无定义的当所有新的

调用页面时,所有变量都会显示范围(会话,应用程序除外)



另外,为ASP.Net应用程序创建

数据库连接的最佳做法是什么?


Thx

解决方案

Brian,


变量确实保留在范围内。但是在调用数据库之后你仍然需要关闭

连接(取决于你正在执行的代码类型

)。


例如数据读取器需要关闭,但是数据适配器负责为您关闭连接。


-

此致,


S. Justin Gengo,MCP

网站开发人员/程序员


免费代码库位于:
www.aboutfortunate.com


混乱中出现了秩序。

Nietzche

" Brian" < SK ***** @ optonline.net>在消息中写道

news:db **************************** @ phx.gbl ... < blockquote class =post_quotes>如果我为页面的每个
请求声明一个模块级别的oledbconnection,那么变量仍在范围内?我在
页面的后续调用中收到
open.executing错误消息。

我认为ASP.Net是无定义的,并且所有变量都在范围内(除了会话,应用程序)在对页面进行新的
调用时?

此外,为ASP.Net应用程序创建数据库连接的最佳做法是什么?

Thx



1。使用ViewState来保持变量状态。

示例ViewState(" myVariable")= myvalue

2.更好地创建数据访问层。

Bin Song

MCP

-----原始消息-----
如果我宣布一个模块级别oledbconnection与页面的每个请求,变量仍然在范围内?我在后续的
页面调用时遇到了错误消息。

我认为ASP.Net是无定义的,所有变量都是范围的(除了范围外)会话,应用程序)在对页面进行新的
调用时?

此外,为ASP.Net应用程序创建数据库连接的最佳做法通常是什么?

Thx



感谢您立即回复...


执行后我会立即关闭连接。我

在所有页面上也有enableViewState = false。我只是

我不明白为什么变量仍然在

后续调用的范围内?


私有函数GetDbConnection()作为OleDbConnection


''检查连接是否已经存在

如果(m_objOleDbConn什么都没有)那么''还没有!

m_strConnectionStr =

ConfigurationSettings.AppSettings(" CONNECT_STR")

m_objOleDbConn =新OleDbConnection

(m_strConnectionStr)

m_objOleDbConn 。打开()

结束如果


''检查状态

如果(不是m_objOleDbConn.State =

ConnectionState.Open)然后

If(不是m_objOleDbConn.State =

ConnectionState.Closed)然后

m_objOleDbConn.Close()

结束如果

m_objOleDbConn =新OleDbConnection

(m_strConnectionStr)

m_objOleDbConn.Open()


结束如果


''返回连接

返回(m_ob jOleDbConn)


结束功能

-----原帖-----
Brian,

变量确实保留在范围内。但是在调用数据库之后你还需要
关闭连接(取决于
你正在执行什么类型的代码)。

数据阅读器需要关闭,但数据适配器
负责为您关闭连接。

-
真诚的,

S. Justin Gengo,MCP
Web开发人员/程序员

免费代码库:
www.aboutfortunate.com

出于混乱而来的订单。Nietzche

" Brian" < SK ***** @ optonline.net>在消息中写道
新闻:db **************************** @ phx.gbl ...

如果我声明模块级oledbconnection与页面的每个
请求,变量仍然在范围内? I

页面的后续调用中获取open.executing错误消息。

我认为ASP.Net是无定义的,并且所有变量都超出范围内的
(除了会话,应用程序)在对页面进行新的
调用时?

此外,为ASP.Net应用程序创建数据库连接的最佳做法是什么?

Thx





If i declare a module level oledbconnection with each
request for the page, the variable remains in scope? I get
open.executing error message on subsequent calls for the
page.

I assumed ASP.Net to be statless and all variables go out
of scope (except session, application) when making new
calls for a page?

Also, what is generally the best practice for creating
database connections for ASP.Net apps?

Thx

解决方案

Brian,

The variable does remain in scope. But you still need to close the
connection after a call to the database (depending on what type of code
you''re executing).

e.g. A data reader needs to be closed, but a data adapter takes care of
closing the connection for you.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Brian" <sk*****@optonline.net> wrote in message
news:db****************************@phx.gbl...

If i declare a module level oledbconnection with each
request for the page, the variable remains in scope? I get
open.executing error message on subsequent calls for the
page.

I assumed ASP.Net to be statless and all variables go out
of scope (except session, application) when making new
calls for a page?

Also, what is generally the best practice for creating
database connections for ASP.Net apps?

Thx



1. Using ViewState to maitain state of variables.
example ViewState("myVariable") = myvalue
2. Better create a data access tier.

Bin Song
MCP

-----Original Message-----
If i declare a module level oledbconnection with each
request for the page, the variable remains in scope? I getopen.executing error message on subsequent calls for the
page.

I assumed ASP.Net to be statless and all variables go out
of scope (except session, application) when making new
calls for a page?

Also, what is generally the best practice for creating
database connections for ASP.Net apps?

Thx
.



Thanks for the immediate reponses...

I do immediately close the connection after execution. I
also have enableViewState = false on all the pages. I just
don''t understand why the variable remains in scope on
subsequent calls?

Private Function GetDbConnection() As OleDbConnection

'' Check whether connection already exists
If (m_objOleDbConn Is Nothing) Then '' Not yet!
m_strConnectionStr =
ConfigurationSettings.AppSettings("CONNECT_STR")
m_objOleDbConn = New OleDbConnection
(m_strConnectionStr)
m_objOleDbConn.Open()
End If

'' Check the State
If (Not m_objOleDbConn.State =
ConnectionState.Open) Then
If (Not m_objOleDbConn.State =
ConnectionState.Closed) Then
m_objOleDbConn.Close()
End If
m_objOleDbConn = New OleDbConnection
(m_strConnectionStr)
m_objOleDbConn.Open()

End If

'' Return Connection
Return (m_objOleDbConn)

End Function

-----Original Message-----
Brian,

The variable does remain in scope. But you still need to close theconnection after a call to the database (depending on what type of codeyou''re executing).

e.g. A data reader needs to be closed, but a data adapter takes care ofclosing the connection for you.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Brian" <sk*****@optonline.net> wrote in message
news:db****************************@phx.gbl...

If i declare a module level oledbconnection with each
request for the page, the variable remains in scope? I get open.executing error message on subsequent calls for the
page.

I assumed ASP.Net to be statless and all variables go out of scope (except session, application) when making new
calls for a page?

Also, what is generally the best practice for creating
database connections for ASP.Net apps?

Thx


.



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

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