2011-08-11 11 views
4

データベースに直接接続する「db」という汎用クラスがあります。そして、以下のように「ExecuteDataReader」と呼ばれる方法があります:SQLDataReaderとCommandBehaviour.CloseConnection

public SqlDataReader ExecuteDataReader(SqlCommand cmd) 
     { 
      try 
      { 
       OpenConnection(); 
       cmd.Connection = conn; 
       cmd.CommandType = CommandType.StoredProcedure;    

       SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

       return dr; 
      } 
      catch (Exception ex) 
      { 
       Utils.Debug(string.Format("Err in {0}.{1} : {2}\nSQL : {3}", this.GetType(), "ExecuteDataReader", ex.Message, cmd.CommandText)); 
       return null; 
      }    
     } 

はその後、私は、データベースに更新するために、10000枚の親レコードと子レコード20000をループリソース集約型のクエリを実行します。そして、私は次のエラーました。これらの問題を解決するために

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. 

を、私は明示的に実行された後dr.Close()を呼び出す必要があります。

static void ProcessAssessmentCriteria(string UnitReference) 
     { 
      SqlCommand cmd = new SqlCommand("TRACKING.DBTool_GetUniqueAssessmentCriteriaByUnitReference"); 
      cmd.Parameters.Add("@UnitReference", SqlDbType.VarChar, 20).Value = UnitReference; 

      SqlDataReader dr = db.ExecuteDataReader(cmd); 

      if (dr.HasRows) 
      { 
       while (dr.Read()) 
       {      
        ProcessDetailAssessmentCriteria(UnitReference, dr["AssessmentRefNumber"].ToString()); 
        Console.WriteLine("---------------"); 
       } 
      } 

      dr.Close(); 
     } 

私の知るところではあるが、CommandBehaviour.CloseConnection()は自動的に接続を閉じます。しかし、それは今閉じていないようです。私を啓発してもらえますか?ありがとう。 MSDN

When the command is executed, the associated Connection object is closed when the associated DataReader object is closed.

から

答えて

3

CommandBehavior.CloseConnectionだから、あなただけが、接続が閉じられているDataReaderを閉じたとき。

+0

ああそうです。私はそれを誤解した。 :) – TTCG

関連する問題