我尝试使用 Dapper 和存储过程并行执行三个相似的 SQL 查询,以在所有查询完成后获得三个相似的结果。
这是我的代码:
public class SomeReport
{
private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["SomeContext"].ToString();
public ReportStatus ReportStatus { get; set; }
public long NetworkServerTime { get; set; }
public string ReportLastErrorMessage { get; set; }
public RowSet[] FirstRowSet { get; set; }
public RowSet[] SecondRowSet { get; set; }
public RowSet[] ThirdRowSet { get; set; }
public Report()
{
NetworkServerTime = 0;
ReportStatus = ReportStatus.NotCreated;
}
public async Task GetReportDataAsync(ReportParameters parameters)
{
DynamicParameters requestParameters = new DynamicParameters();
requestParameters.Add("@sinceDateFilter", parameters.SinceDate?.Date, DbType.DateTime);
requestParameters.Add("@untilDateFilter", parameters.UntilDate?.Date, DbType.DateTime);
requestParameters.Add("@countryId", parameters.CountryId, DbType.Int32);
ReportLastErrorMessage = null;
Task allTasks = null;
var stopWatch = new Stopwatch();
try
{
var firstTask = GetRows("[dbo].[GET_Report_FirstRowSet]", requestParameters);
var secondTask =
GetRows("[dbo].[GET_Report_SecondRowSet]", requestParameters);
var thirdTask =
GetRows("[dbo].[GET_Report_ThirdRowSet]", requestParameters);
allTasks = Task.WhenAll(firstTask, secondTask, thirdTask);
FirstRowSet = await firstTask;
SecondRowSet = await secondTask;
ThirdRowSet = await thirdTask;
}
catch (Exception ex)
{
ReportStatus = ReportStatus.Error;
ReportLastErrorMessage = allTasks?.Exception?.InnerExceptions.Last().Message;
}
但是,当我启动调试器和 SQL Server Profiler 时,我发现在创建与其对应的任务时,查询是按顺序执行的。
如何使查询同时开始运行并并行运行?
慕码人8056858
胡说叔叔
相关分类