[英]NHibernate calling procedure using named query in C#
I am facing some problem while calling procedure using named query in Nhibernate. 我在Nhibernate中使用命名查询调用过程时遇到了一些问题。 I can easily call simple procedure / parametrized procedure but I have procedure which accept three argument, one is varchar(max) type which have guid seperated by commas (this is passed as in parameter to SQL query), other is varchar(50) and last one is bit type.
我可以轻松地调用简单过程/参数化过程,但是我有接受三个参数的过程,一个是varchar(max)类型,其guid用逗号分隔(这作为参数传递给SQL查询),另一个是varchar(50)和最后一个是位类型。
I have following procedure: 我有以下步骤:
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)
I had written following code into hbm file. 我已经将以下代码写入了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>
And for calling this procedure I had written following code. 为了调用此过程,我编写了以下代码。
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>();
Here I had tried all type of combination for passing parameter to procedure using NHibernate but it always crashed. 在这里,我尝试使用NHibernate将参数传递给过程的所有类型的组合,但始终会崩溃。
Try embedding the EXEC of your sql-query inside a BEGIN/END block: 尝试将SQL查询的EXEC嵌入到BEGIN / END块中:
BEGIN
EXEC ar_tARCustomer_ReadAccount_ForBalanceDetail_New .. params ..
END
At least that's how I got it to work in NH 3.2 Mapping.ByCode. 至少这就是我在NH 3.2 Mapping.ByCode中使用它的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.