ASP.NET连接字符串 [英] ASP.NET Connection String

查看:116
本文介绍了ASP.NET连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.NET 2.0应用程序的连接字符串中的一个出现问题。在某些情况下,我会得到 ConnectionString属性尚未初始化的问题,该问题是随机且间歇性地,没有任何原因发生的。

I am having some trouble with one of my ASP.NET 2.0 application's connection string. There are instances that I would get a ConnectionString Property Has not Been Initialized problem which occurs randomly and intermittently and without any reason.

我的连接字符串实际上是来自Web服务,因为不同类型的用户根据他们的用户级别为其提供了不同的连接字符串集。

My Connection string is actually coming from a webservice, because different kinds of users are provided with different sets of connection string depending on their user level.

到目前为止,我所做的就是这样:

What I have done so far goes like this:

我有一个母版页( mstr.page )和后面的相应代码( mstr.page.vb )。

I have a master page (mstr.page) and the corresponding code behind (mstr.page.vb).

在我的母版页中,我最初检索连接字符串并将其存储到会话变量中,即

In my master page, I retrieve initially the connection string and store the same into a session variable, i.e.

Session("ConnString") = "RetrievedConnectionString"

现在在我的一个页面中,让我们说 page1.aspx.vb ,我使用了类(名为 MyClass.vb )并在我的 page1.aspx.vb 中使用。

Now in one of my pages, let us say page1.aspx.vb, I use public shared functions from a class (named MyClass.vb) and use it in my page1.aspx.vb.

有些参考代码:

[MyClass.vb]
Imports System.Web

NameSpace ClassNameSpace
  Public Class Admin
    Protected Shared da as New DataAccess()

    Public Shared Function MYFunction() as String
      'Execute some sql statements here, using the DataAccess
      stringToReturn = Ctype(da.ExecuteScalar("SQLCommand"), String)
      Return stringToReturn
    End Function
  End Class
End NameSpace

[DataAccessClass.vb]
Public Class DataAccess()
  Private m_ConStr As String = ""

  Public Sub New()
    m_ConStr = HttpContext.Current.Session("ConnString")
  End Sub

  'Some methods for SQL statement execution (ExecuteQuery, ExecuteScalar)
End Class

[Page1.aspx.vb]
Imports ClassNameSpace

Protected Sub Page_Load(....) Handles Me.Load
  Dim strValue as String = Admin.MyFunction()
End Sub

我在上面放置了代码,向您展示了事情的发展概况。

I have placed the code above to show you some rough idea of how things are going.

基本上,函数 Admin.MyFunction()有时会失败,因为在数据访问类中,连接字符串似乎丢失了它的值(空白或空)。

Basically, the function Admin.MyFunction() at times fails, because in the data access class, the connection string seems to have lost it's value (either blank or Nothing).

这已经困扰了我好一段时间了。

This has troubled me for quite some time already.

我希望有人可以指出正确的方向来解决这个问题。基本上,我希望所有访问Web应用程序的用户都可以一直维护我的连接字符串,并可以在任何地方使用它。会话变量似乎不是最合适的方法,因为当ASP.NET回收其进程时,会话会丢失。

I hope someone can point me in the right direction to resolve this. Basically, I want my connection string which is retrieved by each user visiting the web application be maintained across all the time and be used anywhere. Session variable does not seem to be the best fit since when the ASP.NET recycles its process, the session is lost.

顺便说一句,我最初是在检索连接字符串通过Web服务的母版页。当条件是会话变量丢失时,我试图在数据访问类中放置相同的检索函数,但是我认为我的应用程序在回收过程中无法连接到Web服务。

By the way, I am retrieving the connectionstring initially via the master page from a web service. I tried to place the same retrieve function in the Data Access class when conditions is that the session variable is lost, but I think my application cannot connect to the Web Service during the recycle process.

对此,任何建议将不胜感激。

Any advice would be greatly appreciated on this.

对此进行更新:

我尝试使用会话变量并将模式设置为State Server,但是显然我正在使用的某些DLL无法序列化,因此我回到了第一个平方。

I tried to use Session Variable and set the mode to State Server, but apparently some DLLs which I am using cannot be serialized, thus I am back to square one.

有更好的方法吗?

推荐答案

要检查的一件事是您的Session是否被破坏。如果您使用(默认)内存中会话,则会话将在ASP.NET工作进程被回收时随时终止。如果这是导致您出现问题的原因,则可能需要考虑使用IIS中的ASP.NET SessionServer或SQL Server作为会话存储。

One thing to check is if your Session is getting clobbered. If your using the (default) in-memory Session, then sessions die anytime an ASP.NET worker process is recycled. If this is causing your issue, you might have to look into using the ASP.NET SessionServer in IIS, or SQL Server as your Session storage.

这篇关于ASP.NET连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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