首页 > 解决方案 > 等待批量插入存储过程完成

问题描述

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

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

注意:当从 SQL Server Management Studio 运行时,如果我在我的操作方法中放置断点,存储过程可以正常工作。

这是我调用存储过程的方式;

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 });
}

我也使用ExecuteSqlRawAsyncwithawait得到相同的结果。

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

谢谢

标签: c#sql-serverstored-procedures.net-core

解决方案


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

所以我改变了

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

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

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


推荐阅读