NHibernate.Exceptions.GenericADOException:无法执行查询
我有一个遗留应用程序(vfp 8),我需要从(无插入)中提取数据。我现在用的是Accnum字段作为主键,在该表中被定义为角色11NHibernate.Exceptions.GenericADOException:无法执行查询
出厂配置:
<?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的探查它表明:
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.
第一件事我会尝试是在运行SQL这一点,看看会发生什么,因为它可能是数据问题。
SELECT 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%确定方言的选择,因为这可能会导致你的问题。
编辑2 NHProf错误在我看来,它认为你的ID应该是一个Int32,因为它看起来像它调用at System.Data.OleDb.OleDbParameterCollection.GetParameter(Int32 index)
。
我想你需要添加到您的映射: -
<id name="Accnum" column="Accnum" type="string" >
注意额外type="string"
正如你所建议的那样,我测试了SQL并且它确实有效。数据库是一个传统的foxpro表。我在Fox中测试了SQL,它运行正常。 – 2012-01-09 01:23:11
请参阅编辑,也使用哪个版本的foxpro? – Rippo 2012-01-09 09:10:19
我正在使用VFP 8.我已经看到http://*.com/questions/4106470/nhibernate-configuration-to-connect-to-visual-foxpro-8-0这让我相信这个尝试。无法在配置中使用proxyfactory.factory_class会导致问题?请参阅编辑 – 2012-01-09 10:45:01
我和我的LINQ查询抛出了同样的错误每当我经过他们一个参数挣扎。如果我没有传递任何参数,并做了session.Query(),他们会正常工作。
我挣扎了几天,但我发现这Nhibernate杰拉票here。它解释了SQLParameters和Iseries Db2提供程序的一个明显问题。
我知道您使用的是不同的提供程序,但您可以从下载最新的Nhibernate核心源代码,构建它并在项目中引用最新版本中受益。它解决了我的问题。
你能分享会话工厂配置吗? – 2012-01-08 04:31:43
确定我已经添加了 – 2012-01-09 01:22:18
如果它可以与MsSql2000Dialect方言一起使用,可以使用 – 2012-01-09 04:15:32