等待大容量插入存储过程完成



我有一个存储过程,它实际上批量插入条目到数据库中,在它完成后,它返回输出参数。

当我从我的。net Core web应用程序调用我的过程时,它不会等待过程完成,而是在下一步重定向页面。如何让代码等待存储过程完成?

注意:当从SQL Server Management Studio运行存储过程时,如果我在我的action方法中设置了一个断点,则存储过程工作得非常好。

下面是我如何调用我的存储过程;

public IActionResult Import(ImportTransactionsVM TransactionsVM)
{
int ReportResult = 0;
DataPathParam = new SqlParameter("@pDataFilePath", FilesHandlerHelper.ImportFile(_hostingEnvironment, TransactionsVM.ReportFile));
FileTypeParam = new SqlParameter("@pFileType", TransactionsVM.BankType);
OutputParam = new SqlParameter("@pOutputValue", SqlDbType.Int)
{
//Direction of this parameter is output.
Direction = ParameterDirection.Output,
Value = 0
};
FormatPathParam = new SqlParameter("@pFormatFilePath", Path.Combine(_hostingEnvironment.ContentRootPath, "Data\ImportFormats\MigsReport.fmt"));
ReportResult = _dbContext.Database
.ExecuteSqlRaw("exec SP_ImportTransactionReports @pDataFilePath, @pFormatFilePath, @pFileType, @pOutputValue OUT", DataPathParam, FormatPathParam, FileTypeParam, OutputParam);

return RedirectToAction("Import", new { result = OutputParam.Value });
}

我还使用了ExecuteSqlRawAsyncawait,结果相同。

而且,存储过程实际上是批量插入记录。

感谢

所以问题是当我在上传过程中复制文件时,导入的文件被锁定了。

所以我改变了

File.CopyTo(new FileStream(FilePath, FileMode.Create));

var _FileStream = new FileStream(FilePath, FileMode.Create);
File.CopyTo(_FileStream);
_FileStream.Close();

感谢@David Browne - Microsoft为我指明了正确的方向。

最新更新