2011-03-08 58 views
0

我有完美的代碼,但如果我選擇了很多結果,它會拋出GenericADOException。GenericADOException的標準太多,無法執行查詢

存在被由Hibernate genereted SQL:

SELECT this_.Value as y0_, 
     this_1_.OrderNumber as y1_, 
     count(*) as y2_ 
FROM DotaznikySpokojenosti.dbo.QuestionRateDTO this_ 
     inner join DotaznikySpokojenosti.dbo.QuestionDTO this_1_ 
     on this_.QuestionRateID=this_1_.ID 
WHERE this_1_.Questinary in (@p0, @p1, @p2, @p3, ... , @p2876) 
GROUP BY this_.Value, this_1_.OrderNumber 
ORDER BY y1_ asc, y0_ asc 

問題是,如果有更多的然後aproximetly 2000參數(不完全)在 「WHERE」 部分 「IN」 的一部分。如果更少,它的作品完美。

這是NHibernate還是SQL Server的問題?

我該怎麼辦?

生成更短的查詢並在C#中完成它?

現在我有相對好的代碼,如果我這樣做,我怕它會是一個小小的混亂的代碼。

這是代碼,它只是爲了說明。我的意思是沒有問題:

public class AdultDAO : ANHibernateDAO<AdultDTO, Nullable<Int32>>, IAdultDAO 
    {  
     public ResultQuestinaryDTO Evaluate(
      IEnumerable<LocalityDTO> _Localities, 
      IEnumerable<DepartmentDTO> _Departments, 
      bool _IncludeNullDepartment, 
      IEnumerable<int> _Months, 
      IEnumerable<int> _Years, 
      IEnumerable<TypeHospitalization> _Types, 
      IEnumerable<Sex> _Sex, 
      IEnumerable<int> _Old, 
      string _Version) 
     { 
      // Session 
      ISession _Session = NHibernateSessionManager.Instance.GetSession(); 
      //Criterium 
      ICriteria _Criteria = _Session.CreateCriteria(typeof(AdultDTO)); 

      // Where clause 
      _Criteria = _Criteria.Add(Expression.InG("Locality", _Localities.ToArray())); 
      _Criteria = _Criteria.Add(Expression.InG("YearHospitalization", _Years.ToArray())); 
      _Criteria = _Criteria.Add(Expression.InG("MonthHospitalization", _Months.ToArray())); 
      _Criteria = _Criteria.Add(Expression.InG("TypeHospitalization", _Types.ToArray())); 
      _Criteria = _Criteria.Add(Expression.InG("Sex", _Sex.ToArray())); 
      _Criteria = _Criteria.Add(Expression.InG("Old", _Old.ToArray())); 
      _Criteria = _Criteria.Add(Expression.Eq("Version", _Version)); 

      if ((_Departments.Count() > 0) && (_IncludeNullDepartment)) 
      { 
       _Criteria = _Criteria.Add(Expression.Or(
        Expression.InG("Department", _Departments.ToArray()), 
        Expression.IsNull("Department"))); 
      } 
      if ((_Departments.Count() > 0)) 
       _Criteria = _Criteria.Add(Expression.InG("Department", _Departments.ToArray())); 
      else 
       _Criteria = _Criteria.Add(Expression.IsNull("Department")); 

      _Criteria.Add(Expression.Conjunction()); 
      // Return only id 
      _Criteria.SetProjection(Projections.Id()); 

      // QuestionBoolDTO query 
      ICriteria _CriteriaBool = ResultSetHelper.CreateCriteria(
       _Session, 
       typeof(QuestionBoolDTO), 
       Transformers.AliasToBean<ResultQuestionBoolDTO>(), 
       _Criteria.List()) 
       .AddOrder(new Order("Value", true)); 

      // QuestionRateDTO query 
      ICriteria _CriteriaRate = ResultSetHelper.CreateCriteria(
       _Session, 
       typeof(QuestionRateDTO), 
       Transformers.AliasToBean<ResultQuestionRateDTO>(), 
       _Criteria.List()) 
       .AddOrder(new Order("Value", true)); 

      // QuestionSetDTO query 
      ICriteria _CriteriaSet = ResultSetHelper.CreateCriteria(
       _Session, 
       typeof(QuestionSetDTO), 
       Transformers.AliasToBean<ResultQuestionSetDTO>(), 
       _Criteria.List()); 

      // Return result set 


      // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
      // THERE IS CRITICAL PART WHICH THROW EXCEPTION: 
      // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 

      return new ResultQuestinaryDTO 
       (
        _CriteriaRate.List<ResultQuestionRateDTO>(), 
        _CriteriaSet.List<ResultQuestionSetDTO>(), 
        _CriteriaBool.List<ResultQuestionBoolDTO>(), 
        _Criteria.List().Count, 
        AdultDTO.GetTemplate(_Version) 
       ); 
     } 


public class ResultQuestinaryDTO 
    { 
     private IEnumerable<ResultQuestionBoolDTO> _ResultQuestionBool;   
     private IEnumerable<ResultQuestionSetDTO> _ResultQuestionSet;   
     private IEnumerable<ResultQuestionRateDTO> _ResultQuestionRate;     
     private int _CountQuestinary = 0; 

     public ResultQuestinaryDTO(
      IEnumerable<ResultQuestionRateDTO> ResultQuestionRate, 
      IEnumerable<ResultQuestionSetDTO> ResultQuestionSet, 
      IEnumerable<ResultQuestionBoolDTO> ResultQuestionBool, 
      int CountQuestinary, 
      QuestinaryTemplateWrapper QuestinaryTemplateWrapper 
      ) 
     { 
      _ResultQuestionBool = ResultQuestionBool; 
      _ResultQuestionRate = ResultQuestionRate; 
      _ResultQuestionSet = ResultQuestionSet; 
      this.CountQuestinary = CountQuestinary; 
      this.QuestinaryTemplateWrapper = QuestinaryTemplateWrapper; 
     } 
    } 
+0

我根本不知道NHibernate--但是通常當動態SQL變得太長時,你會用盡字符串空間。結果是,最後一些字符丟失 - 並且生成的T-SQL字符串不再有效。你應該嘗試找出內部使用的字符串變量需要多長時間(看起來像8000就是通常的varchar最大值)。 – 2011-03-08 09:40:08

+0

看看異常的細節,看看錯誤代碼是什麼。 http://msdn.microsoft.com/en-us/library/ms177682.aspx表示代碼8623或8632可能由太多值引起 – 2011-03-08 10:20:05

回答

1

如果您使用SQL Server,那麼它會限制SQL中參數的使用。最大參數數量爲2100.