2012-01-08 156 views
4

我有一個遺留應用程序(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. 
+0

你能分享會話工廠配置嗎? – 2012-01-08 04:31:43

+0

確定我已經添加了 – 2012-01-09 01:22:18

+0

如果它可以與MsSql2000Dialect方言一起使用,可以使用 – 2012-01-09 04:15:32

回答

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%確定方言的選擇,因爲這可能會導致你的問題。

我把它你看過thisthis

編輯2 NHProf錯誤在我看來,它認爲你的ID應該是一個Int32,因爲它看起來像它調用at System.Data.OleDb.OleDbParameterCollection.GetParameter(Int32 index)

我想你需要添加到您的映射: -

<id name="Accnum" column="Accnum" type="string" > 

注意額外type="string"

+0

正如你所建議的那樣,我測試了SQL並且它確實有效。數據庫是一個傳統的foxpro表。我在Fox中測試了SQL,它運行正常。 – 2012-01-09 01:23:11

+0

請參閱編輯,也使用哪個版本的foxpro? – Rippo 2012-01-09 09:10:19

+0

我正在使用VFP 8.我已經看到http://stackoverflow.com/questions/4106470/nhibernate-configuration-to-connect-to-visual-foxpro-8-0這讓我相信這個嘗試。無法在配置中使用proxyfactory.factory_class會導致問題?請參閱編輯 – 2012-01-09 10:45:01

2

我和我的LINQ查詢拋出了同樣的錯誤每當我經過他們一個參數掙扎。如果我沒有傳遞任何參數,並做了session.Query(),他們會正常工作。

我掙扎了幾天,但我發現這Nhibernate傑拉票here。它解釋了SQLParameters和Iseries Db2提供程序的一個明顯問題。

我知道您使用的是不同的提供程序,但您可以從下載最新的Nhibernate核心源代碼,構建它並在項目中引用最新版本中受益。它解決了我的問題。

相關問題