我必须使用 Dapper 从 C# 执行以下 SQL 查询:
var sqlQuery = @"
;WITH CalculatedData AS (
select
b.Guid1C as guid
,b.Name as name
,SUM(ct.TotalSold) as total_Sold
,SUM(ct.TotalIncome) as total_Income
,SUM(ct.SelfCost) as self_Cost
,SUM(ct.TotalProfit) as total_Profit
,CASE WHEN (SUM(ct.TotalProfit) > 0 AND SUM(ct.TotalIncome) > 0) THEN (SUM(ct.TotalProfit) / SUM(ct.TotalIncome) * 100) ELSE 0 END as gross_Profit_Margin
from CalculatedTransactions ct with(nolock)
inner join
Brands b with(nolock) on ct.BrandGuid = b.Guid1C
where ct.TransactionDate >= @fromDate and ct.TransactionDate <= @toDate
group by b.Guid1C, b.Name
)
SELECT total_count = count(1) OVER(), full_total_icome = SUM(total_Income) OVER(), *
FROM CalculatedData WHERE 1=1
ORDER BY name ASC
OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY";
我通过以下方式传递动态参数:
var @params = new DynamicParameters();
@params.Add("fromDate", from);
@params.Add("toDate", to);
@params.Add("skip", skip);
@params.Add("take", take);
var result = await connection.QueryAsync(sqlQuery, @params);
问题是查询仅在 C# 程序中运行超时。
where ...
子句,它就有效;WITH
子句的查询,它会起作用知道可能是什么问题吗?
您遇到的问题可能与 Dapper 中的
CommandTimeout
设置有关
观看它https://code-maze.com/dotnet-handling-commandtimeout-with-dapper/