NHibernate.Exceptions.GenericADOException:无法执行查询

我有一个遗留应用程序(vfp 8),我需要从中提取数据(无插入)。 我使用Accnum字段作为主键,它在表中定义为字符11。

工厂配置:

    NHibernate.Connection.DriverConnectionProvider NHibernate.Dialect.GenericDialect NHibernate.Driver.OleDbDriver Provider=VFPOLEDB.1;Data Source=C:\Analysis\Quantium\development\RD warehouse\_RDAUWH\Data;Collating Sequence=MACHINE false   

这是我的映射文件:

            

class级:

 public class CustMast { private string _accnum; public virtual string Accnum { get { return _accnum; } set { _accnum = value; } } private string _fullname; public virtual string Fullname { get { return _fullname; } set { _fullname = value; } } private string _add; public virtual string Add { get { return _add; } set { _add = value; } } private string _state; public virtual string State { get { return _state; } set { _state = value; } } } 

以下是获取记录的代码:

 public CustMast GetByAccnum(String accnum) { using (ISession session = NHibernateHelper.OpenSession()) { CustMast custMast = session .CreateCriteria(typeof(CustMast)) .Add(Restrictions.Eq("Accnum", accnum)) .UniqueResult(); return custMast; } } 

完整的错误是:

 NHibernate.Exceptions.GenericADOException : could not execute query [ SELECT this_.Accnum as Accnum0_0_, this_.Fullname as Fullname0_0_, this_.Add as Add0_0_, this_.State as State0_0_ FROM CustMast this_ WHERE this_.Accnum = ? ] Name:cp0 - Value:00059337444 [SQL: SELECT this_.Accnum as Accnum0_0_, this_.Fullname as Fullname0_0_, this_.Add as Add0_0_, this_.State as State0_0_ FROM CustMast this_ WHERE this_.Accnum = ?] ----> System.IndexOutOfRangeException : Invalid index 0 for this OleDbParameterCollection with Count=0. - d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:1590 

运行NHibernate Profiler它显示:

 WARN: reflection-optimizer property is ignored out of application configuration file. WARN: System.IndexOutOfRangeException: Invalid index 0 for this OleDbParameterCollection with Count=0. at System.Data.OleDb.OleDbParameterCollection.RangeCheck(Int32 index) at System.Data.OleDb.OleDbParameterCollection.GetParameter(Int32 index) at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index) at NHibernate.Driver.DriverBase.ExpandQueryParameters(IDbCommand cmd, SqlString sqlString) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Driver\DriverBase.cs:line 235 at NHibernate.AdoNet.AbstractBatcher.ExpandQueryParameters(IDbCommand cmd, SqlString sqlString) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\AdoNet\AbstractBatcher.cs:line 232 at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1152 ERROR: Invalid index 0 for this OleDbParameterCollection with Count=0. 

每当我传递一个参数时,我都在努力解决我的linq查询丢失相同的错误。 如果我没有传递任何参数并做了session.Query()他们会工作正常。

几天我一直在努力,但我在这里找到了这张Nhibernate jira票。 它解释了SQLParameters和Iseries Db2提供程序的明显问题。

我知道您正在使用其他提供商,但您可能只需下载最新的Nhibernate核心源代码,构建它并在项目中引用最新版本即可获益。 它解决了我的问题。

我要尝试的第一件事是在SQL中运行它,看看会发生什么,因为它可能是一个数据问题。

选择this_.Accnum为Accnum0_0_,this_.Fullname为Fullname0_0_,this_.Add为Add0_0_,this_.State为State0_0_ FROM CustMast this_ WHERE this_.Accnum =’00059337444′

Accnum列是否在数据库中定义为字符串类型(varchar,nvarchar等)?

编辑确定下一步是实际确认发送到FoxPro的SQL。 您需要设置日志记录(或下载NHProf的试用版)以确定SQL是否正确。 您的设置和代码看起来正确,但我不是100%肯定选择方言,因为这可能会导致您的问题。

我认为你已经看到了这个和这个 。

Edit2 NHProf错误在我看来,它认为你的Id应该是一个Int32,因为它看起来像at System.Data.OleDb.OleDbParameterCollection.GetParameter(Int32 index)调用。

我想你需要将它添加到你的映射: –

  

注意附加的type="string"