我有完美的代码,但是如果我选择了很多结果,它会抛出GenericADOException。
有一个由Hibernate创建的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
问题是,如果在“WHERE”部分的“IN”部分中有更多的aproximetly 2000参数(不完全)。如果更少,它的工作完美。
是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 :(得分:1)
如果使用SQL Server,则会限制SQL中参数的使用。最大参数数量为2100。