asp.net中的连接超时错误

时间:2017-11-07 17:09:09

标签: c# asp.net sql-server

我遇到了一些问题,每次点击按钮都会出现此错误:

  

超时已过期。在获得a之前经过了超时时间   从游泳池连接。这可能是因为所有人都集中了   正在使用连接并达到最大池大小。

下一分钟我得到一个错误。它会引发异常

public List<TransactionIssues> GetAllTransactions()
            {
                SqlConnection connection = new SqlConnection(connectionString);
                SqlCommand command = new SqlCommand();
                List<TransactionIssues> transIssues = new List<TransactionIssues>();
                command.CommandText = "issue_sp_getallunpostedissues";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = connection;
                SqlDataReader rdrObj;

                try
                {
                    connection.Open();
                    rdrObj = command.ExecuteReader();
                    while (rdrObj.Read())
                    {
                        TransactionIssues issues = new TransactionIssues();
                        issues.requisitionNumber = rdrObj.GetString(0);
                        issues.transactionDate = rdrObj.GetDateTime(1);
                        //issues.expense_acccount.account_desc = rdrObj.GetString(2);
                        //issues.expense_acccount.index = rdrObj.GetInt16(3);
                        issues.inventory_acccount.index = rdrObj.GetInt32(2);
                        issues.inventory_acccount.account_desc = rdrObj.GetString(3);
                        issues.docNumber = rdrObj.GetString(4);
                        issues.docType = rdrObj.GetString(5);
                        issues.items = getTransItemByRquisition(rdrObj.GetString(4));
                        transIssues.Add(issues);
                    }
                    rdrObj.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
                return transIssues;
            }

1 个答案:

答案 0 :(得分:2)

您应该处置SqlConnection以防止内存泄漏和未关闭的连接。 &#34;使用&#34;关键字负责呼叫&#34;最后&#34;从而关闭你的联系:

public List<TransactionIssues> GetAllTransactions()
        {
            using(var connection = new SqlConnection(connectionString))
            {
                using(var command = new SqlCommand())
                {
                    List<TransactionIssues> transIssues = new List<TransactionIssues>();
                    command.CommandText = "issue_sp_getallunpostedissues";
                    command.CommandType = CommandType.StoredProcedure;
                    command.Connection = connection;

                        connection.Open();
                        using(SqlDataReader rdrObj = command.ExecuteReader())
                        {
                            while (rdrObj.Read())
                            {
                                TransactionIssues issues = new TransactionIssues();
                                issues.requisitionNumber = rdrObj.GetString(0);
                                issues.transactionDate = rdrObj.GetDateTime(1);
                                //issues.expense_acccount.account_desc = rdrObj.GetString(2);
                                //issues.expense_acccount.index = rdrObj.GetInt16(3);
                                issues.inventory_acccount.index = rdrObj.GetInt32(2);
                                issues.inventory_acccount.account_desc = rdrObj.GetString(3);
                                issues.docNumber = rdrObj.GetString(4);
                                issues.docType = rdrObj.GetString(5);
                                issues.items = getTransItemByRquisition(rdrObj.GetString(4));
                                transIssues.Add(issues);
                            }
                        }
                        rdrObj.Close();

                    return transIssues;
                }
            }
        }