2011-03-11 41 views
0

我遇到問題。我有一個循環設置爲兩個函數的統計分析。在循環中第一次在這兩個函數中都很好。第二次通過循環第二個函數不返回任何結果。循環Oracle數據讀取器返回不一致的結果

我不明白爲什麼第一次通過循環我從DataReader的每隨後的時間通過結果我得到什麼(datareader.hasRows == false

任何幫助將是巨大的。先謝謝你。

下面是對循環結構和第二函數的代碼的大綱(即失敗)

startDate = new DateTime(2010, 1, 1); 
for(int i = 0; i < 20; i++) 
{ 
    for(int j = 1; j <= 12; j++) 
    { 
     endDate = new DateTime(2010, j, 1); 
     schedule = new Schedule(); 
     scheduleXml = string.Empty; 

     ISession newSession = session.SessionFactory.OpenSession(); 
     scheduleXml = GetScheduleXml(startDate, endDate, locationIds, newSession); 
     schedule = GetSchedule(schedule, startDate, endDate, locationIds, newSession); 
     newSession.Close(); 
    } 
} 

public static Schedule GetSchedule(Schedule schedule, DateTime startDate, DateTime endDate, int locationIds, ISession session) 
{ 
    string sqlGetActivitiesTreatmentsRegimens = // Custom SQL Select Statement (I have verified it returns results) 
    using (OracleCommand comm = new OracleCommand(sqlGetActivitiesTreatmentsRegimens, (OracleConnection)session.Connection)) 
    { 
     try 
     { 
      OracleParameter plantIdParam = comm.CreateParameter(); 
      plantIdParam.DbType = DbType.Int32; 
      plantIdParam.Value = plantId; 
      comm.Parameters.Add(plantIdParam); 

      OracleParameter startDateParam = comm.CreateParameter(); 
      startDateParam.DbType = DbType.DateTime; 
      startDateParam.Value = startDate; 
      comm.Parameters.Add(startDateParam); 

      OracleParameter endDateParam = comm.CreateParameter(); 
      endDateParam.DbType = DbType.DateTime; 
      endDateParam.Value = endDate; 
      comm.Parameters.Add(endDateParam); 

      using(OracleDataReader dr = comm.ExecuteReader()) 
      { 
       while(dr.Read()) 
       { 
        // does stuff here 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("Source:GetRegimensEx:" + ex.Message, ex.InnerException); 
     } 
    } 
    return new Schedule(); 
} 

回答

1

據我所看到的,你發佈的代碼是明顯的語法錯誤除了罰款。該錯誤要麼在GetScheduleXml中,要麼在執行ISession的類的實現中。

0

同意丹尼爾。另外,您在using語句中使用session.Connection,但方法簽名中的參數是newSession。

DataReader處於使用狀態,以便處理關閉。不需要額外的.Close()調用。

+0

我在想這個參數是一樣的,但是如果第一個循環迭代起作用,那麼它不應該是這樣嗎? – 2011-03-11 19:04:12

+0

我一般都認同,但是因爲函數是靜態的,我們無法看到GetScheduleXml或者實現了ISession的類對靜態變量做了什麼,我們不知道。 – 2011-03-11 19:14:18

+0

嗯......這給了我幾個想法。我將研究如何實現ISession。 本質上,GetScheduleXml從存儲過程中讀取並返回基於存儲過程輸出的xml字符串。 – Mark 2011-03-11 19:38:51