Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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/7/sql-server/24.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#SQL连接池_C#_Sql Server_Asp.net Mvc - Fatal编程技术网

C#SQL连接池

C#SQL连接池,c#,sql-server,asp.net-mvc,C#,Sql Server,Asp.net Mvc,大家好 我遇到了一个特殊的问题,我的C#应用程序的连接池空间不足。我已经编写了一个自定义SqlHelper类,它应该返回每个类使用的相同连接 但这似乎并不像预期的那样有效。在调试时,似乎返回了相同的连接,除非该连接已经关闭。我认为我正在实例化SqlHelper类的多个实例,并将其更改为使用一个会话变量,该变量为每个类返回相同的SqlHelper实例 如果您能在这方面提供帮助,我将不胜感激,因为我看不出这为什么不能如预期的那样工作 下面是我的SqlHelper类: public class Sql

大家好

我遇到了一个特殊的问题,我的C#应用程序的连接池空间不足。我已经编写了一个自定义SqlHelper类,它应该返回每个类使用的相同连接

但这似乎并不像预期的那样有效。在调试时,似乎返回了相同的连接,除非该连接已经关闭。我认为我正在实例化SqlHelper类的多个实例,并将其更改为使用一个会话变量,该变量为每个类返回相同的SqlHelper实例

如果您能在这方面提供帮助,我将不胜感激,因为我看不出这为什么不能如预期的那样工作

下面是我的SqlHelper类:

public class SqlHelper {

    /// <summary>
    /// Private connection string which reads the connection string from the web.config file
    /// </summary>
    private string _connectionString {
        get {
            //Get the connection string from the web.config
            return ConfigurationManager.ConnectionStrings["defaultConn"].ConnectionString;
        }
    }

    /// <summary>
    /// Internal connection
    /// </summary>
    private SqlConnection _connection;
    /// <summary>
    /// Public connection that instantiates the connection
    /// </summary>
    public SqlConnection Connection {
        get {
            //Check if the connection has not been created
            if (_connection == null)
                _connection = new SqlConnection(_connectionString);
            //Check if the connection is closed, if it is, open it back up again
            if (_connection.State == ConnectionState.Closed || _connection.State == ConnectionState.Broken)
                _connection.Open();
            //Return the connection
            return _connection;
        }
    }

    /// <summary>
    /// Executes SQL query with(out) parameters which will return a SqlDataReader to access results
    /// </summary>
    /// <param name="sqlCommand"></param>
    /// <param name="para"></param>
    /// <returns>SqlDataReader filled with results</returns>
    public SqlDataReader GetSqlReader(string sqlCommand, SqlParameter[] para = null) {
        //Build up the command with the connection
        SqlCommand command = new SqlCommand(sqlCommand, Connection);
        //Add all the parameters to the sql command
        if (para != null)
            foreach (SqlParameter param in para)
                command.Parameters.Add(param);
        return command.ExecuteReader();
    }

当您将
\u connection
对象作为字段时,使您的类成为可丢弃的。

您在哪里关闭/处理您的连接?我建议在您的
SqlHelper
类中实现
IDisposable
接口以关闭连接并释放资源。SQL server具有出色的池管理-不要做任何事情服务器做得更好。正如其他地方所建议的,打开一个连接,使用它,在需要时关闭它。使用将其包装在
中以确保其正常运行。实际上,您可能希望在每次使用后打开和关闭SQL Server连接。通常这样做会导致SQL执行延迟,但默认情况下SQL Server使用“连接池”,这意味着过去的连接并没有真正关闭。这是一种自动发生的奇迹,发生得很快。另外,因为您总是关闭连接,所以您永远不必担心使用的连接超过实际需要的数量。有关详细信息,请阅读“SQL Server连接池”。
public class BaseController : Controller
{
    //protected SqlHelper sqlHelper = new SqlHelper();

    protected SqlHelper sqlHelper {
        get {
            SqlHelper helper = Session["SqlHelper"] as SqlHelper;
            if (helper == null) {
                helper = new SqlHelper();
                Session["SqlHelper"] = helper;
            }
            return helper;
        }
    }