我正在编写C#代码并使用LINQ和一些存储过程,我在打开和关闭连接时很小心,但是我一直收到此错误。

Timeout expired.
The timeout period elapsed prior to obtaining a connection from the pool.
This may have occurred because all pooled connections were in use and max pool size was reached.


我的代码可以正常工作,除了会出现此错误,我该怎么办?

感谢您的任何想法。

 public static List<int> GetIslemIdleribySPbyCariId(int cariId)
    {
        string connString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString;

        SqlConnection sqlConn = new SqlConnection(connString);
        sqlConn.Open();

        List<int> islemidleri = new List<int>();
        islemidleri.Clear();

        SqlCommand cmd;
        cmd = new SqlCommand("GetIslemIdleri", sqlConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@CARIID", cariId));

        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                islemidleri.Add(reader.GetInt32(0));

            }
            cmd.Parameters.Clear();
        }

        sqlConn.Close();

        return islemidleri;

    }
    /// <summary>
    /// SP kullanarak dovizturlerini döndürür
    /// </summary>
    /// <returns>string listesi döndürür için döviz türleri var TL, USD vs.</returns>
    public static List<string> GetDovizTurleribySP()
    {
        string connString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString;

        SqlConnection sqlConn = new SqlConnection(connString);
        sqlConn.Open();

        List<string> dovizTanimlari = new List<string>();

        string commandGetDovizTanimlari = "EXEC GetDovizTanimlari";
        SqlCommand cmd;
        cmd = new SqlCommand(commandGetDovizTanimlari, sqlConn);

        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                dovizTanimlari.Add(reader.GetString(0));
            }

        }
        return dovizTanimlari;

    }

最佳答案

从您的代码中,您不会在GetDovizTurleribySP函数中关闭连接。我建议您使用using语句来确保即使发生异常也关闭连接。

它可能看起来像这样

public static List<string> GetDovizTurleribySP()
{
  string connString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString;

  using (SqlConnection sqlConn = new SqlConnection(connString))
  {
    sqlConn.Open();

    List<string> dovizTanimlari = new List<string>();

    string commandGetDovizTanimlari = "EXEC GetDovizTanimlari";
    SqlCommand cmd;
    cmd = new SqlCommand(commandGetDovizTanimlari, sqlConn);

    using (var reader = cmd.ExecuteReader())
    {
      while (reader.Read())
      {
        dovizTanimlari.Add(reader.GetString(0));
      }

    }
    return dovizTanimlari;
  }
}

关于c# - 超时时间已到,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5460008/

10-11 00:41