![](/img/trans.png)
[英]NHibernate - NHibernate.Exceptions.GenericADOException: could not execute query
[英]NHibernate.Exceptions.GenericADOException : could not execute query
我有一个旧版应用程序(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>
这是我的映射文件:
<?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>
班级:
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<CustMast>();
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等)?
编辑 OK下一步是实际确认SQL被发送到FoxPro。 您将需要设置日志记录(或下载NHProf的试用版),以查明SQL是否正确。 您的设置和代码看起来正确,但是我不确定100%选择方言,因为这可能会给您带来麻烦。
Edit2在我看来,NHProf错误认为它的ID应该是Int32,因为它看起来像是at System.Data.OleDb.OleDbParameterCollection.GetParameter(Int32 index)
调用。
我认为您需要将此添加到映射中:
<id name="Accnum" column="Accnum" type="string" >
请注意其他type="string"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.