代码如下: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!"
{
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!"
是不是写成
queryObject.SetParameter(i-1, _values[i-1]);
<![CDATA[from PanGu.Net.Test.Data.Employee employee where employee.EmployeeCode = :EmployeeCode]]>
</query>
<![CDATA[from PanGu.Net.Test.Data.Employee employee where employee.EmployeeCode = ?]]>
</query>
{
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;
}