小编Vis*_*l P的帖子

如何在 Dot Net Core 3.1 中使用 FromSqlInterpolated/Database.ExecuteSqlInterpolated 执行带有输出参数的存储过程?

我想在 Dot Net core 3.1 中执行一个带有输出参数的存储过程。我正在使用类的ExecuteSqlInterpolated扩展方法DatabaseFacade

获取员工人数的 C# 代码。

string deptName="IT";
int? employeeCount = null;
Database.ExecuteSqlInterpolated($"exec dbo.usp_GetEmpCountByDept {deptName}, {employeeCount} out");
Run Code Online (Sandbox Code Playgroud)

执行后employeeCount 是null-1是返回值。由于有些人要求存储 proc 代码来重现该问题,因此我将 proc 存储如下

CREATE PROCEDURE usp_GetEmpCountByDept
@Dept nvarchar(20),
@EmpCount int Output
AS
BEGIN
SELECT @EmpCount = COUNT(Id)
FROM [dbo].[Employees] 
WHERE Department = @Dept
END
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core .net-core-3.1

6
推荐指数
2
解决办法
4398
查看次数

标签 统计

.net ×1

.net-core ×1

.net-core-3.1 ×1

c# ×1