共享数据库变量的正确方法(asp.net)
我一直在使用以下代码共享数据库变量:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此错误并不是告诉您删除公共共享变量。相反,它让您知道可以创建
Vars
类的新实例,即使它仅包含Shared
成员。要解决此问题,请定义一个私有构造函数:这将阻止任何代码在类本身之外创建
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 onlyShared
members. To resolve the issue, define a private constructor:This will prevent any code creating an instance of the
Vars
class outside of the class itself.这是您班级中唯一的代码吗?
另外,您不应创建全局(静态)
SqlConnection
。只需按需创建SqlConnection
和SqlCommand
对象即可。连接池将确保一次仅建立一个物理数据库连接。从这里的情况来看,它不是线程安全的(例如,如果两个人同时发出请求,事情就会变得非常糟糕)。
Is that the only code in your class?
Also, you should not create a global (static)
SqlConnection
. Simply create theSqlConnection
andSqlCommand
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).