什么是使用这种静态类的潜在问题 [英] What are the potential issues using this static class

查看:114
本文介绍了什么是使用这种静态类的潜在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的示例code:

Here is my sample code:

public static class MySqlHelper
{
    private static string constring = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;

    public static int ExecuteNonQuery(string mysqlquery)
    {
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(mysqlquery, conn);
        int result;

        try
        {
            conn.Open();
            result= cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
        return result;
    }
}

用法: MySqlHelper.ExecuteNonQuery(选择客户*);

我想知道用这个静态类的问题。

I would like to know the issues using this static class.

提到我可以改变我的课<一个href=\"http://stackoverflow.com/questions/6155650/writing-my-own-provider-class-in-asp-net/6155708#6155708\">here但我一直在使用的几个网站,这个类,我需要几天时间来改变它在每一个地方,并测试它。

I can change my class as mentioned here but I have been using this class in couple of websites and I will need couple days to change it in every place and test it out.

感谢您的任何投入。

编辑:更新了code。这是否使的答案差异提供?对不起,我应该张贴在开头。

Updated the code. Does that make difference on the answers provided? Sorry, I should have posted in the beginning.

推荐答案

我假设在执行过程中的连接字符串并没有改变(你可能要使它只读)。由于在这个问题没有显示其他共享状态,没有真正的问题。

I am assuming the connection string doesnt change during execution (you might want to make it readonly). Since there is no other shared state shown in the question, there are no real problems.

不过,如果您有任何共享状态,你有一个巨大的线程问题。如果你有共同的连接的你有一个更大的问题。

However, if you have any shared state you have a huge threading problem. And if you have shared connections you have an even bigger problem.

但作为写的,没有显著静态字段:没问题

But as written, with no significant static fields: no problem.

这篇关于什么是使用这种静态类的潜在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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