Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “如何使用”;使用;在我的连接课上_C#_Asp.net - Fatal编程技术网

C# “如何使用”;使用;在我的连接课上

C# “如何使用”;使用;在我的连接课上,c#,asp.net,C#,Asp.net,我想在我的代码中像这样使用using forSqlConnection using (SqlConnection Con = new SqlConnection()) 但是我在一个connections类中使用了dictionary进行了几个连接。这是我的连接类的代码 public static class Connections { public static string BillReport { get; set; } public static string DuesR

我想在我的代码中像这样使用using for
SqlConnection

using (SqlConnection Con = new SqlConnection())
但是我在一个
connections
类中使用了
dictionary
进行了几个连接。这是我的
连接
类的代码

public static class Connections
{
    public static string BillReport { get; set; }
    public static string DuesReport { get; set; }
    public static string GeneralReport { get; set; }
    public static string PendingReport { get; set; }
    public static string RadiologyReport { get; set; }
    public static string HistoReport { get; set; }
    public static string SMSAPI { get; set; }
    public static string SenderId { get; set; }
    public static string SMSUserName { get; set; }
    public static string SMSContent { get; set; }
    public static string CompanyAddress { get; set; }
    public static string CompanyMobile { get; set; }

    private static Dictionary<string, SqlConnection> _Connection = new Dictionary<string, SqlConnection>();
    public static Dictionary<string, SqlConnection> Connection
    {
        get { return _Connection; }
        set { _Connection = value; }
    } 
     public static void Init(string Name)
     {
         string user = HttpContext.Current.Session["UserName"].ToString();

         //INIT YOUR CONNECTION PROPERTY HERE
         if (!Connection.ContainsKey(user))
         {
             Connection.Add(user, new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings[Name].ConnectionString));
         }
         else
         {
             //if (Connection[user] == null)
             //{
                 Connection[user] = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings[Name].ConnectionString);
             //}
         }
     }
}

但是我怎样才能使用

呢?如果你真的想,只需使用
连接。连接[用户名]
。返回相同的
SqlConnection
。然而,这将处理它。您的模式/设计有缺陷,因为ADO.NET具有连接池


您还可以添加自己的包装对象,该对象实现了
IDisposable

,通常不需要在代码中缓存连接。除非您已经进行了测量,并且确信创建连接对象确实是一个瓶颈,否则您得到的唯一结果就是增加了复杂性。您的类本身就是bug。你根本不需要那本字典。由于连接池,打开一个新连接不会花费任何费用。为什么您认为首先需要缓存连接?为什么不使用创建新连接对象的标准实践?如果希望使用该类进行测试,则可以创建一个小的帮助器方法来基于名称检索连接字符串-您的网站会崩溃,因为您试图
.Open()
已释放的连接。最糟糕的情况是,您试图更换连接,却忘记了正确地处理它们,最终由于难以检测到的打开的事务、锁等而导致性能缓慢下降。在所有情况下,您将使用比需要多得多的连接。连接池可以为多个用户重置和重用同一连接。现在,每个用户将打开另一个connectionHi@PanagiotisKanavos我的应用程序运行在本地主机上,多台计算机通过IP访问它。所以我用了字典。这不是个好主意吗?请建议我。这将导致缓存释放连接。由于连接池,OP的类没有什么用处。没有必要缓存连接最好的指导是“不要这样做”
Connections.Connection[UserName].Open();