在C#中使用命名查询的NHibernate调用过程

时间:2013-05-21 10:20:11

标签: c# sql nhibernate fluent-nhibernate

我在Nhibernate中使用命名查询调用过程时遇到了一些问题。我可以很容易地调用简单的过程/参数化过程但是我有接受三个参数的过程,一个是varchar(max)类型,它有guid用逗号分隔(这在参数中传递给SQL查询),另一个是varchar(50)和最后一个是位类型。

我有以下程序:

CREATE PROCEDURE [dbo].[ar_tARCustomer_ReadAccount_ForBalanceDetail]    

 @CustomerList varchar(max),    
 @AsOfDate  varchar(50),    
 @PostedOnly  bit    

AS    



SET NOCOUNT ON;    

Declare @ErrorNumber int    
Declare @SQL   varchar(max)    
Declare @PostedInvoice varchar(50)    
Declare @PostedPayment varchar(200)    

SET @PostedInvoice = ''    
SET @PostedPayment=  ''    
IF (@PostedOnly = 1)    
BEGIN    
 SET @PostedInvoice = ' And ari.fIsPosted = 1 '    
 SET @PostedPayment = ' And pay.fIsPosted =     
   CASE pay.fPaymentType    
     WHEN ''CM'' THEN 0    
     WHEN ''EPD'' THEN 0    
     ELSE 1    
   END'    
END    

Set @SQL =     
'    
select * from    
(    
 Select Distinct    
   co.fName as fTrxCompanyName, prop.fName as fTrxPropertyName, arc.fName as fTrxCustomerName,       
   cast(ari.fARInvoiceNumber  as varchar(10)) as fTrxNumber,    
   ari.fInvoiceDescription as fTrxDescription,    
   ari.fIsVoided as fTrxIsVoided,    
   CASE fInvoiceType    
     WHEN ''Credit Memo'' THEN fBalance    
     WHEN ''Early Payment Discount'' THEN fBalance    
     ELSE fAmount    
   END as fTrxAmount    
 From tARInvoice ari    
  Inner Join tARCustomer arc on arc.fCustomerID = ari.fCustomerID    
  LEFT OUTER JOIN tSCCompany co on co.fCompanyID = arc.fCompanyID    
  LEFT OUTER JOIN tSCProperty prop on prop.fPropertyID = arc.fPropertyID     
 Where ari.fCustomerID in (' + @CustomerList + ') and    
    ari.fInvoiceDate >= convert(datetime,'+ '''' + @AsOfDate + '''' + ') and    
    ari.fInvoiceType <> ''Recurring Template''' + @PostedInvoice + '    

 Union All    

 Select  Distinct    
   co.fName as fTrxCompanyName, prop.fName as fTrxPropertyName, cust.fName as fTrxCustomerName,         
   ''Payment'' as fTrxType,     
   null,         
   pay.fComments as fTrxDescription,    
   pay.fIsVoided as fTrxIsVoided,    
            pay.fAmount as fTrxAmount    
 From tARPayment pay    
  Left Outer Join tARPaymentInvoice payinv On pay.fPaymentID = payinv.fPaymentID    
  Left Outer Join tARInvoice inv On payinv.fInvoiceID = inv.fARInvoiceID    
  Left Outer Join tARCustomer cust on cust.fCustomerID = pay.fCustomerID    
  LEFT OUTER JOIN tSCCompany co on co.fCompanyID = cust.fCompanyID    
  LEFT OUTER JOIN tSCProperty prop on prop.fPropertyID = cust.fPropertyID      
 Where (inv.fCustomerID IN (' + @CustomerList +')    
  OR (pay.fCustomerID IN (' + @CustomerList +') AND (inv.fCustomerID NOT IN (' + @CustomerList +') OR inv.fCustomerID IS NULL))    
  OR ((pay.fCustomerID IS NULL OR pay.fCustomerID NOT IN (' + @CustomerList +')) AND inv.fCustomerID IN (' + @CustomerList +')))    
  and pay.fEffectiveDate >= convert(datetime,' + '''' + @AsOfDate + '''' + ')'        + @PostedPayment + '    

 Union All    

-- Get the voided payments only with the reversed payment amount and the voided description    
 Select  Distinct    
   co.fName as fTrxCompanyName, prop.fName as fTrxPropertyName, cust.fName as fTrxCustomerName,       
   ''Payment'' as fTrxType,     
   null,    
   pay.fVoidedDate as fTrxAddedDate,    
   pay.fUnappliedAmount as fTrxBalance,    
   cast(pay.fCheckNumber as varchar(10)) as fTrxNumber,    
   ''Voided Early Payment Discount #'' + cast(pay.fCheckNumber as varchar(10)) as fTrxDescription,    
   pay.fIsVoided as fTrxIsVoided,    
            (-1 * pay.fAmount) as fTrxAmount    
 From tARPayment pay    
  Left Outer Join tARPaymentInvoice payinv On pay.fPaymentID = payinv.fPaymentID    
  Left Outer Join tARInvoice inv On payinv.fInvoiceID = inv.fARInvoiceID    
  Left Outer Join tARCustomer cust on cust.fCustomerID = pay.fCustomerID    
  LEFT OUTER JOIN tSCCompany co on co.fCompanyID = cust.fCompanyID    
  LEFT OUTER JOIN tSCProperty prop on prop.fPropertyID = cust.fPropertyID      
 Where (inv.fCustomerID IN (' + @CustomerList +')    
  OR (pay.fCustomerID IN (' + @CustomerList +') AND (inv.fCustomerID NOT IN (' + @CustomerList +') OR inv.fCustomerID IS NULL))    
  OR ((pay.fCustomerID IS NULL OR pay.fCustomerID NOT IN (' + @CustomerList +')) AND inv.fCustomerID IN (' + @CustomerList +')))    
  and pay.fIsVoided = 1    
  and pay.fVoidedDate >= convert(datetime,' + '''' + @AsOfDate + '''' + ')'     
   + @PostedPayment + '    
) X    
Where fTrxAmount IS NOT NULL    
Order By fTrxCompanyName, fTrxPropertyName, fTrxAddedDate    
'    
Exec (@SQL)    
--print (@SQL)    

RETURN (@@Error) 

我已将以下代码写入hbm文件。

<sql-query name="ar_tARCustomer_ReadAccount_ForBalanceDetail_New" callable="true">
    <!--<query-param name="CustomerList" type="VARCHAR(max)" />
    <query-param name="AsOfDate" type="string" />
    <query-param name="PostedOnly" type="bool" />-->
    <return class="tARCustomer">
      <return-property column="fTrxCompanyName" name="fTrxCompanyName" />
      <return-property column="fTrxPropertyName" name="fTrxPropertyName" />
      <return-property column="fTrxCustomerName" name="fTrxCustomerName" />
      <return-property column="fCustomerID" name="fTrxCustomerID" />
      <return-property column="fTrxSourceID" name="fTrxSourceID" />
      <return-property column="fInvoicePosted" name="fInvoicePosted" />
      <return-property column="fTrxDate" name="fTrxDate" />
      <return-property column="fPostedDate" name="fPostedDate" />
      <return-property column="fTrxType" name="fTrxType" />
      <return-property column="fTrxDueDate" name="fTrxDueDate" />
      <return-property column="fTrxAddedDate" name="fTrxAddedDate" />
      <return-property column="fTrxBalance" name="fTrxBalance" />
      <return-property column="fTrxDescription" name="fTrxDescription" />
      <return-property column="fTrxIsVoided" name="fTrxIsVoided" />
      <return-property column="fTrxAmount" name="fTrxAmount" />
    </return>

    exec ar_tARCustomer_ReadAccount_ForBalanceDetail_New
    @CustomerList=:CustomerList,
    @AsOfDate=:AsOfDate,
    @PostedOnly=:PostedOnly
  </sql-query>

为了调用这个程序,我写了下面的代码。

var Customerbalance = Session.GetNamedQuery("ar_tARCustomer_ReadAccount_ForBalanceDetail_New")
                                           .SetParameter("CustomerList", "'''bced443a-ce86-4675-bca6-ae5646ad9c2e'' , ''bced443a-ce86-4675-bca6-ae5646ad9c2e'''")
                                           .SetParameter("AsOfDate", "10/1/2012")
                                           .SetParameter("PostedOnly", PostedOnly)
                                           .SetResultTransformer(new AliasToBeanResultTransformer(typeof(tARCustomer))).List<tARCustomer>();

在这里,我尝试了所有类型的组合,使用NHibernate将参数传递给过程,但它总是崩溃。

1 个答案:

答案 0 :(得分:0)

尝试在SEGIN / END块中嵌入sql-query的EXEC:

BEGIN
    EXEC ar_tARCustomer_ReadAccount_ForBalanceDetail_New .. params ..
END

至少我是如何让它在NH 3.2 Mapping.ByCode中工作的。