Nhibernate Named Query 问题(100分,分不够在加)

GreatSolar 2009-01-19 03:38:27
代码如下:DAO层

try
{
IQuery queryObject = session.GetNamedQuery(_queryName);
if (_values != null)
{
for (int i = 1; i <= _values.Length; i++)
{
queryObject.SetParameter(i, _values[i-1]);
}
}
return queryObject.List();
}
catch (QueryException ex)
{
return null;
}

测试代码入下:
[Test]
public void FindByNameQueryByEmployeeCode()
{
object[] values = { "Lubo" };
IList list = employeeDao.FindByNamedQuery("GetEmployeeByEmployeeCode","Lubo");
System.Console.WriteLine(list.Count);
}

测试过程中:
异常入下:
IndexOutOfRangeException
"Remember that ordinal parameters are 1-based!"
...全文
149 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
海洋里的鳄鱼 2009-01-19
  • 打赏
  • 举报
回复
try
{
IQuery queryObject = session.GetNamedQuery(_queryName);
if (_values != null)
{
for (int i = 0; i <_values.Length; i++)
{
queryObject.SetParameter(i, _values[i]);
}
}
return queryObject.List();
}
catch (QueryException ex)
{
System.Console.WriteLine(ex.Message);
return null;
}
海洋里的鳄鱼 2009-01-19
  • 打赏
  • 举报
回复
<query name="GetEmployeeByEmployeeCode">
<![CDATA[from PanGu.Net.Test.Data.Employee employee where employee.EmployeeCode = ?]]>
</query>
GreatSolar 2009-01-19
  • 打赏
  • 举报
回复
Named Query定义如下:

<query name="GetEmployeeByEmployeeCode">
<![CDATA[from PanGu.Net.Test.Data.Employee employee where employee.EmployeeCode = :EmployeeCode]]>
</query>
GreatSolar 2009-01-19
  • 打赏
  • 举报
回复
没人回答么?
急用啊。
GreatSolar 2009-01-19
  • 打赏
  • 举报
回复
我试过
不行
优途科技 2009-01-19
  • 打赏
  • 举报
回复
帮顶吧。
Teng_s2000 2009-01-19
  • 打赏
  • 举报
回复
queryObject.SetParameter(i, _values[i-1]);
是不是写成
queryObject.SetParameter(i-1, _values[i-1]);

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧