2011-01-08 60 views
1

這是我的代碼NHibernate的.NET對於Oracle 10g「無法執行查詢」

我想知道想錯了嗎?

public IList listdataserviceplan(String custid) 
{ 
    using (ISession session = NHibernateHelper.OpenSession()) 
    { 
     string query = 」 select a.ServicePlanId as ServicePlanId , 
           a.ServiceDetail as ServiceDetail,」 
      + 」 a.DateServiceFix as DateServiceFix ,a.DateService as DateService,」 
      + 」 a.CaseNotSupport as CaseNotSupport ,a.ServiceChangeName as ServiceChangeName,」 
      + 」 a.DateServiceNew as DateServiceNew ,a.MaterialChange as MaterialChange,」 
      + 」 a.ServiceGuarantee as ServiceGuarantee ,a.ServiceMaintenance as ServiceMaintenance,」 
      + 」 a.ServiceCharge as ServiceCharge」 
      + 」 from BicIsu.Core.Domain.ServicePlan as a」 
      + 」 where 1=1″ 
      + 」 and a.CustId = ‘」 + custid + 「‘ 」 
      + 」 order by a.ServicePlanId」; 

     var cons = session.CreateQuery(query).List(); 
     return cons; 
    } 
} 
+0

如果您在NHibernate之外手動運行它,查詢是否可以正常工作? – 2011-01-08 03:25:34

回答

0

我看不出錯誤,但我敢打賭,它是與那些超級複雜的魔法字符串。嘗試:

string query = @"select a.ServicePlanId, 
         a.ServiceDetail, 
         a.DateServiceFix, 
         a.DateService, 
         a.CaseNotSupport, 
         a.ServiceChangeName, 
         a.DateServiceNew, 
         a.MaterialChange, 
         a.ServiceGuarantee, 
         a.ServiceMaintenance, 
         a.ServiceCharge 
       from ServicePlan as a 
       where 1=1 
       and a.CustId = :custId 
       order by a.ServicePlanId"; 

var result = session.CreateQuery(query) 
        .SetParameter("custId", custid) 
        .List(); 

僅供參考,Ad-hoc mapping with NHibernate.

0

可能不是最好的方式來寫一個HQL查詢。爲什麼不使用StringBuilder類。 它會是這樣的:

var hqlQuery= new StringBuilder(); 
hqlQuery.Append("select a from ClassA"); 
hqlQuery.AppendFormat("where a.Id={0}",idVal); 

return session.List<ClassA>(hqlQuery.toString()); 

我知道這不是答案。但肯定會幫助你的代碼。

現在談談你的問題。什麼是你得到的錯誤。該查詢是否正在觸發或您是否遇到異常?