c# - 在 sql 中运行但不在 .Net 中运行的查询
问题描述
我正在尝试在返回单个记录的 c# .net 代码中编写 sql 查询。当我使用提供的参数在 sql 中执行时它运行正确,但从.Net 代码执行时不返回任何行。不确定是什么问题 ?下面的代码解释了控制流程
.Net 代码
public class FedExciseExpense
{
public float DomicileId { get; set; }
public float CoveragePolicyTypeId { get; set; }
public bool Is953D { get; set; }
public decimal? Value { get; set; }
}
public List<FedExciseExpense> GetFedExciseTaxExpenses(int industryId, int domicileId,int coveragePolicyTypeId, bool is953D)
{
var sql = @"select distinct gm.geographyBaseId as DomicileId, fedt.is953d as Is953D, fedt.coveragePolicyTypeId as CoveragePolicyTypeId, fedt.value as Value
from fedExciseTax fedt
join GeographyGroup gg on gg.geographyGroupId = fedt.geographyGroupId
join GeographyName gn on gn.geographyGroupId = gg.geographyGroupId and gn.geographyNameId = fedt.geographyNameId
join GeographyMap gm on gm.geographyNameId = gn.geographyNameId
join industryMap iMatch on iMatch.industryNameId = fedt.industryNameId
join industryMap iInput on iInput.industryBaseId = iMatch.industryBaseId
where gm.geographyBaseId = @domicileId and iInput.industryNameId = @industryId and fedt.coveragePolicyTypeId = @coveragePolicyTypeId and fedt.is953d = @is953D";
var param = new { domicileId, industryId, coveragePolicyTypeId , is953D };
return WithCache(sql, param, () =>
{
using (var conn = ConnectionFactory.GetConnection())
{
conn.Open();
var data = conn.Query(sql, param).Select(dyn => new FedExciseExpense()
{
DomicileId = (int)dyn.domicileId,
CoveragePolicyTypeId = (int)dyn.coveragePolicyTypeId,
Is953D = (int)dyn.Is953d ,
Value = (decimal?)dyn.Value
}).ToList();
conn.Close();
return data;
}
});
}
private static T WithCache<T>(string query, object parameters, Func<T> execution)
{
var key = query;
if (parameters != null)
{
key += "@" + JsonConvert.SerializeObject(parameters, Formatting.None);
}
return (T)QueryCache.GetOrAdd(key, _ => execution());
}
Actual query
declare @domicileId int = 69,
@industryId int = 52,
@coveragePolicyTypeId int = 1,
@is953D int = 1
select distinct gm.geographyBaseId as DomicileId, fedt.is953d as Is953D, fedt.coveragePolicyTypeId as CoveragePolicyTypeId, fedt.value as Value from fedExciseTax fedt join GeographyGroup gg on gg.geographyGroupId = fedt.geographyGroupId join GeographyName gn on gn.geographyGroupId = gg.geographyGroupId and gn.geographyNameId = fedt.geographyNameId join GeographyMap gm on gm.geographyNameId = gn.geographyNameId join industryMap iMatch on iMatch.industryNameId = fedt.industryNameId join industryMap iInput on iInput.industryBaseId = iMatch.industryBaseId
where gm.geographyBaseId = @domicileId and iInput.industryNameId = @industryId and fedt.coveragePolicyTypeId = @coveragePolicyTypeId and fedt.is953d = @is953D
核心价值
下面是在WithCache方法中初始化的key的值
select distinct gm.geographyBaseId as DomicileId, fedt.is953d as Is953D, fedt.coveragePolicyTypeId as CoveragePolicyTypeId, fedt.value as Value
from fedExciseTax fedt
join GeographyGroup gg on gg.geographyGroupId = fedt.geographyGroupId
join GeographyName gn on gn.geographyGroupId = gg.geographyGroupId and gn.geographyNameId = fedt.geographyNameId
join GeographyMap gm on gm.geographyNameId = gn.geographyNameId
join industryMap iMatch on iMatch.industryNameId = fedt.industryNameId
join industryMap iInput on iInput.industryBaseId = iMatch.industryBaseId
where gm.geographyBaseId = @domicileId and iInput.industryNameId = @industryId and fedt.coveragePolicyTypeId = @coveragePolicyTypeId and fedt.is953d = @is953D@{"domicileId":69,"industryId":52,"coveragePolicyTypeId":0,"is953D":1}
只接受不带参数的查询的查询
private static List<T> RunQuery<T>(string query, object parameters = null)
{
return WithCache(query, parameters, () => RunNoCacheQuery<T>(query, parameters));
}
private static List<T> RunNoCacheQuery<T>(string query, object parameters = null)
{
using (var conn = ConnectionFactory.GetConnection())
{
conn.Open();
var data = conn.Query<T>(query, parameters).ToList();
conn.Close();
return data;
}
}
解决方案
推荐阅读
- powershell - 在 power shell 中,如何将我在文件夹中执行的操作复制到其他文件夹?
- javascript - 如何迭代数组的数组
- windbg - 从 Microsoft Dev Dashboard 的崩溃中获取符号
- c - C - 直接使用地址访问内存?
- r - 在 R 中有效地删除带有字符变量的行
- javascript - 如果 CSRF Token 通过 Postman 我们可以在不提交表单的情况下获取数据吗
- sql - 将数据从 Excel 传输到 SQL 多个索引表
- firebase - 函数执行耗时 60002 毫秒,完成状态为:'timeout'
- c++ - 带有子项目的 Qt 项目管理:从另一个项目访问类
- xml - XPath 使用单个路径选择多个节点