代码如下: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!"

解决方案 »

  1.   

    queryObject.SetParameter(i, _values[i-1]); 
    是不是写成
    queryObject.SetParameter(i-1, _values[i-1]); 
      

  2.   

    Named Query定义如下: <query name="GetEmployeeByEmployeeCode">
       <![CDATA[from PanGu.Net.Test.Data.Employee employee where employee.EmployeeCode = :EmployeeCode]]>
     </query>
      

  3.   

    <query name="GetEmployeeByEmployeeCode">
        <![CDATA[from PanGu.Net.Test.Data.Employee employee where employee.EmployeeCode = ?]]>
      </query>
      

  4.   

                   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;
                    }