开发者

Too much criteria, GenericADOException, could not execute query

I have code which works perfect, BUT if I select to much results, it throw GenericADOException.

There is SQL which is genereted by Hibernate:

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

Problem is, if there is more then aproximetly 2000 parameters (not exactly) in "IN" part in "WHERE" part. If less, Its works perfect.

It is NHibernate or SQL Server problem?

What can I do?

Generate more shorter queryes and complete it in C#?

Now I have relativly nice code, if i do it I afraid that it will be a little messy code.

This is code, it is only for illustration. I mean that there is no problem:

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()));
  开发者_JS百科          _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;
        }
    }


If you use SQL Server then it limits usage of parameters in SQL. Maximum number of parameters is 2100.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜