我使用dapper进行了SQL查询,并且返回空结果。但是,如果我运行SQL事件探查器并执行我看到的请求,结果将不会为空。
这是我的代码
public class Parameters
{
public DateTime? SinceDate { get; set; }
public DateTime? UntilDate { get; set; }
public byte NetworkId { get; set; } = 7;
}
var parameters = new Parameters()
{
SinceDate = startDate,
UntilDate = endDate,
NetworkId = networkId,
};
DynamicParameters requestParameters = new DynamicParameters();
requestParameters .Add("@sinceDateFilter", parameters.SinceDate, DbType.DateTime);
requestParameters .Add("@untilDateFilter", parameters.UntilDate, DbType.DateTime);
requestParameters .Add("@networkId", parameters.networkId, DbType.Byte);
using (var secondConn = new SqlConnection(ConnectionString))
{
var query = secondConn.Query<ReportRow>(
"[dbo].[GET_Report] @sinceDateFilter, @untilDateFilter, @NetworkId",
requestParameters,
commandTimeout: 500
);
Groups = query.ToList();
}
public class ReportRow
{
public string Network { get; set; }
public ulong Clicks { get; set; }
public decimal Profit { get; set; }
}
在调试器中,我看到“组”为空,但是如果运行sql profiler,则看到此代码发出的请求是
exec sp_executesql N'[dbo].[GET_Report] @sinceDateFilter, @untilDateFilter, @networkId',N'@sinceDateFilter datetime,@untilDateFilter datetime, @networkId tinyint',@sinceDateFilter='2019-04-01 00:00:00',@untilDateFilter='2019-04-02 00:00:00',@networkId=7
如果我从Management Studio执行此操作,则会看到包含数据的表:
+----------+--------+--------+ | Network | Clicks | Profit | +----------+--------+--------+ | Network1 | 38835 | 0.45 | | Network2 | 38835 | 0.35 | +----------+--------+--------+
答案 0 :(得分:0)
如果要通过存储过程执行。
您可以尝试设置commandType
参数。
使用CommandType.StoredProcedure
using (var secondConn = new SqlConnection(ConnectionString))
{
var query = secondConn.Query<ReportRow>(
"[dbo].[GET_Report]",
requestParameters,
commandTimeout: 500,
commandType: CommandType.StoredProcedure
);
Groups = query.ToList();
}