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

P H*_*ans 5 c# nhibernate

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

出厂配置:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<reflection-optimizer use="false" />
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.GenericDialect</property>
<property name="connection.driver_class">NHibernate.Driver.OleDbDriver</property>
<property name="connection.connection_string">Provider=VFPOLEDB.1;Data Source=C:\Analysis\Quantium\development\RD warehouse\_RDAUWH\Data;Collating Sequence=MACHINE</property>
<property name="show_sql">false</property>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

这是我的映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="RDLabels"
               namespace="RDLabels.Domain">

  <class name="CustMast">
    <id name="Accnum" column="Accnum" type="string">
    <generator class="assigned"/>
    </id>
    <property name="Fullname" />
    <property name="Add" />
    <property name="State" />
  </class>  
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

班上:

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是获取记录的代码:

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

完整的错误是:

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
Run Code Online (Sandbox Code Playgroud)

运行 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.
Run Code Online (Sandbox Code Playgroud)

Rip*_*ppo 0

我首先尝试的是在 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 等)?

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

我想你已经看过这个这个了

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

我认为您需要将其添加到您的映射中:-

<id name="Accnum" column="Accnum" type="string" >
Run Code Online (Sandbox Code Playgroud)

注意附加的type="string"