c# - 等待批量插入存储过程完成
问题描述
我有一个存储过程,它实际上将条目批量插入数据库,完成后,它返回输出参数。
当我从 .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 });
}
我也使用ExecuteSqlRawAsync
withawait
得到相同的结果。
此外,存储过程实际上是批量插入记录。
谢谢
解决方案
所以问题是当我在上传过程中复制文件时,正在导入的文件被锁定。
所以我改变了
File.CopyTo(new FileStream(FilePath, FileMode.Create));
至
var _FileStream = new FileStream(FilePath, FileMode.Create);
File.CopyTo(_FileStream);
_FileStream.Close();
感谢@David Browne - Microsoft为我指明了正确的方向。
推荐阅读
- c# - 在蓝色和绿色之间徘徊会产生灰色?Unity, 2d Sprite.color
- r - 为多级混合效应模型汇总(和最大化)数据
- javascript - MERN Stack:使用 Redux 存储用户是否登录
- ssas - 基于strtomember访问前一个成员
- javascript - 在 express 中将绝对 url 路径传递给车把视图
- swift - 结合 Semaphore 来限制 .notify 的异步请求和调度组与 defer 和 App 冻结
- android - 亚行以退出代码 1 退出
- java - 如何在 Java 中使用 BufferedReader 在 .txt 文件中查找特定字符串
- java - 错误:由 OutputStream.flush() 引起的“nativeDrain 中没有错误”
- c# - 在一个时间点监听两个事件的方法的执行时间c#