我有一個存儲過程使用這個腳本在SQL工作室管理快速測試時返回2個記錄:NHibernate的GetNamedQuery執行存儲過程,但返回空列表
declare @route_id_param as varchar(10), @start_time as datetime, @start_date as datetime, @end_date as datetime
set @start_time = GETDATE()
set @start_date = CONVERT(DATETIME,'10/26/2013',101)
set @end_date = CONVERT(DATETIME,'12/26/2020',101)
exec dbo.sp_get_deactivation_list @companyId=1, @startDate = @start_date, @endDate = @end_date;
select execution_time_in_ms = DATEDIFF(millisecond, @start_time, getdate())
GO
當我試圖從NHibernate的執行同一個存儲過程,使用下面的映射文件,我得到0結果。
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
assembly="HGT.IridiumAirtime.Service"
namespace="HGT.IridiumAirtime.Service.Model">
<sql-query name="GetDeactivationList" callable="true">
<query-param name="companyId" type="int"/>
<query-param name="startDate" type="DateTime"/>
<query-param name="endDate" type="DateTime"/>
<!--<return class="Activation">
<return-property column="MobileId" name="MobileId" />
<return-property column="RadioAddress" name="RadioAddress" />
<return-property column="DeactivationDate" name="DeactivationDate" />
</return>-->
exec [sp_get_deactivation_list] @companyId=:companyId, @startDate=:startDate, @endDate=:endDate
</sql-query>
</hibernate-mapping>
是執行所說的存儲過程的方法是:
public IEnumerable<TOut> ExecuteStoredProcedure<TOut>(string procedureName, IList<SqlParameter> parameters)
{
IEnumerable<TOut> result;
using (var session = _sessionFactory.OpenSession())
{
var query = session.GetNamedQuery(procedureName);
foreach (var parameter in parameters)
{
query.SetParameter(parameter.ParameterName, parameter.Value);
}
AddStoredProcedureParameters(query, parameters);
result = query.List<TOut>();
}
return result;
}
我甚至產生了這種激活類型的新映射文件,即使沒有相關的表,但NHibernate的不返回正確的結果。在過程的映射文件中取消註釋類型映射定義不會改變結果。我在這裏錯過了什麼嗎?
我沒有得到任何例外,所以它看起來像程序正在執行。
UPDATE:1刪除呼叫AddStoredProcedureParameters並用的方法體代替。
更新2添加存儲過程:
if OBJECT_ID ('dbo.[sp_get_deactivation_list]', 'P') is not null
drop procedure dbo.[sp_get_deactivation_list];
go
create procedure [dbo].[sp_get_deactivation_list]
@companyId int,
@startDate DateTime,
@endDate DateTime
as
begin
select
assMobileRadio.Mobile_ID as MobileId,
tblRadioinfo.Radio_Address as RadioAddress,
tblRadioinfo.Deactivation_Date as DeactivationDate
from tblRadioinfo
left join assMobileRadio
on tblRadioinfo.Radio_ID = assMobileRadio.Radio_ID
where tblRadioinfo.Radio_Type_ID in (2, 4, 7)
and tblRadioinfo.Company_ID = @companyId
and tblRadioinfo.Deactivation_Date <= @endDate
and tblRadioinfo.Deactivation_Date >= @startDate
and tblRadioinfo.Radio_Address in (select IMEI from [airtime_cdrs] where Effective_Date > @startDate and Effective_Date < @endDate)
from airtimes_cte for xml path('')),1,1,''))
ORDER BY tblRadioinfo.Deactivation_Date
end
+1,用於參考SQL事件探查器。由於我在Express上,我使用了https://expressprofiler.codeplex.com/中的ExpressProfiler,它確認正在執行以下操作:exec sp_executesql N'exec [sp_get_deactivation_list] @ companyId = @ p0,@ startDate = @ p1, @ endDate = @ p2',N'@ p0 int,@ p1 datetime,@ p2 datetime',@ p0 = 1,@ p1 ='2013-10-26 00:00:00',@ p2 ='2020-12 -16 00:00:00' 去。仍然沒有輸出。你爲什麼要做query.List
我有一個類似的存儲過程,名爲GetTransmissionSum通過NHibernate成功執行。當執行這一個時,Express Profiler給出31的CPU,3788的讀數和41ms的持續時間。但是,執行GetDeactivationList時,CPU爲0,讀取爲3,持續時間爲0ms。當我在Studio Management Express中對GetDeactivationList執行測試腳本時,CPU爲125,讀取時間爲3991,持續時間爲124ms。 –
當我rexecute正在傳遞給數據庫的查詢,它返回0的計數。嗯。 –