共享数据库变量的正确方法(asp.net)

发布于 2024-08-29 19:58:44 字数 1518 浏览 12 评论 0 原文

我一直在使用以下代码共享数据库变量:

Namespace DataAccessVariables
    Public Class Vars
        Public Shared s As String
        Public Shared con As String = WebConfigurationManager.ConnectionStrings("Dev").ToString()
        Public Shared c As New SqlConnection(con)
        Public Shared x As New SqlCommand(s, c)
    End Class
End Namespace

然后将其导入到我的项目中,如下所示:

Imports DataAccessVariables.Vars

当我使用 FXCop 检查站点时,我收到此消息:

Error, Certainty 90, for StaticHolderTypesShouldNotHaveConstructors
{
    Target       : DBVars  (IntrospectionTargetType)
    Resolution   : "Remove the public constructors from 'Vars'."
    Help         : http://msdn2.microsoft.com/library/ms182169(VS.90).aspx  (String)
    Category     : Microsoft.Design  (String)
    CheckId      : CA1053  (String)
    RuleFile     : Design Rules  (String)
    Info         : "Instances of types that define only static members 
                   do not need to be created. Many compilers will automatically 
                   add a public default constructor if no constructor 
                   is specified. To prevent this, adding an empty private 
                   constructor may be required."
    Created      : 2010/04/20 01:25:16 PM  (DateTime)
    LastSeen     : 2010/04/21 07:17:46 AM  (DateTime)
    Status       : Active  (MessageStatus)
    Fix Category : Breaking  (FixCategories)
}

如果我从声明中删除“公共共享”,则变量为没有在我的页面中找到。谁能告诉我正确的分享方式?

多谢, 菲尔.

I have been sharing database variables using the following code:

Namespace DataAccessVariables
    Public Class Vars
        Public Shared s As String
        Public Shared con As String = WebConfigurationManager.ConnectionStrings("Dev").ToString()
        Public Shared c As New SqlConnection(con)
        Public Shared x As New SqlCommand(s, c)
    End Class
End Namespace

I then import this to my project like this:

Imports DataAccessVariables.Vars

When I check the site with FXCop, I get this message:

Error, Certainty 90, for StaticHolderTypesShouldNotHaveConstructors
{
    Target       : DBVars  (IntrospectionTargetType)
    Resolution   : "Remove the public constructors from 'Vars'."
    Help         : http://msdn2.microsoft.com/library/ms182169(VS.90).aspx  (String)
    Category     : Microsoft.Design  (String)
    CheckId      : CA1053  (String)
    RuleFile     : Design Rules  (String)
    Info         : "Instances of types that define only static members 
                   do not need to be created. Many compilers will automatically 
                   add a public default constructor if no constructor 
                   is specified. To prevent this, adding an empty private 
                   constructor may be required."
    Created      : 2010/04/20 01:25:16 PM  (DateTime)
    LastSeen     : 2010/04/21 07:17:46 AM  (DateTime)
    Status       : Active  (MessageStatus)
    Fix Category : Breaking  (FixCategories)
}

If I remove the 'Public Shared' from the declarations, then the variables are not picked up in my pages. Can anyone show me the correct way of sharing them?

Thanks a lot,
Phil.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉絮湮 2024-09-05 19:58:44

此错误并不是告诉您删除公共共享变量。相反,它让您知道可以创建 Vars 类的新实例,即使它仅包含 Shared 成员。要解决此问题,请定义一个私有构造函数:

Private Sub New()
End Sub

这将阻止任何代码在类本身之外创建 Vars 类的实例。

This error isn't telling you to remove the Public Shared variables. Instead it's letting you know that it is possible to create a new instance of your Vars class, even though it includes only Shared members. To resolve the issue, define a private constructor:

Private Sub New()
End Sub

This will prevent any code creating an instance of the Vars class outside of the class itself.

浅忆 2024-09-05 19:58:44

这是您班级中唯一的代码吗?

另外,您不应创建全局(静态)SqlConnection。只需按需创建 SqlConnectionSqlCommand 对象即可。连接池将确保一次仅建立一个物理数据库连接。

从这里的情况来看,它不是线程安全的(例如,如果两个人同时发出请求,事情就会变得非常糟糕)。

Is that the only code in your class?

Also, you should not create a global (static) SqlConnection. Simply create the SqlConnection and SqlCommand objects on-demand. Connection pooling will ensure that only one physical database connection is made at a time.

The way you've got it here, it's not thread-safe (if two people make a request at the same time, for example, things are going to get really screwy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文