[英]How to execute stored procedure with Output parameters using FromSqlInterpolated/Database.ExecuteSqlInterpolated in Dot Net Core 3.1?
I would like to execute a stored procedure with output parameter in Dot Net core 3.1.我想在 Dot Net core 3.1 中执行带有输出参数的存储过程。 I am using the
ExecuteSqlInterpolated
extension method of a DatabaseFacade
class.我正在使用
DatabaseFacade
类的ExecuteSqlInterpolated
扩展方法。
C# code to get employee's count.获取员工人数的 C# 代码。
string deptName="IT";
int? employeeCount = null;
Database.ExecuteSqlInterpolated($"exec dbo.usp_GetEmpCountByDept {deptName}, {employeeCount} out");
After execution employeeCount is null
and -1
is the return value.执行后employeeCount为
null
, -1
为返回值。 As some people requested stored proc code for reproducing the issue,I have stored proc as below由于有些人要求存储过程代码来重现问题,我将过程存储如下
CREATE PROCEDURE usp_GetEmpCountByDept
@Dept nvarchar(20),
@EmpCount int Output
AS
BEGIN
SELECT @EmpCount = COUNT(Id)
FROM [dbo].[Employees]
WHERE Department = @Dept
END
I have found other way which is working for me我找到了其他适合我的方法
Add Nuget package Microsoft.Data.SqlClient添加 Nuget 包 Microsoft.Data.SqlClient
Use ExecuteSqlRaw method instead改为使用 ExecuteSqlRaw 方法
Below is the code下面是代码
int? employeeCount = null;
string deptName="IT";
// Use Microsoft.Data.SqlClient namespace for SqlParameter.Visual studio will suggest "system.data.sqlclient" which does not work
var deptNameSQLParam = new Microsoft.Data.SqlClient.SqlParameter("@Dept", deptName);
var employeeCountSQLParam = new Microsoft.Data.SqlClient.SqlParameter("@EmpCount", SqlDbType.Int) { Direction = ParameterDirection.Output };
Database.ExecuteSqlRaw("exec dbo.usp_GetEmpCountByDept @Dept={0}, @EmpCount={1} out", deptNameSQLParam, employeeCountSQLParam);
if (employeeCountSQLParam.Value != DBNull.Value)
{
employeeCount = (int)employeeCountSQLParam.Value;
}
change this改变这个
string deptName="IT";
int? employeeCount = null;
Database.ExecuteSqlInterpolated($"exec dbo.usp_GetEmpCountByDept {deptName}, {employeeCount} out");
to this (worked for me with .NET 5, don't know about earlier)对此(使用 .NET 5 为我工作,之前不知道)
string deptName="IT";
// make explicit SQL Parameter
var output = new SqlParameter("@EmployeeCount", SqlDbType.Int) { Direction = ParameterDirection.Output };
// use output parameter instead
Database.ExecuteSqlInterpolated($"exec dbo.usp_GetEmpCountByDept {deptName}, {output} out");
// assign output
int? employeeCount = output.Value;
ExecuteSqlInterpolated Executes the given SQL against the database and returns the number of rows affected. ExecuteSqlInterpolated 对数据库执行给定的 SQL 并返回受影响的行数。
You cannot expect data to be returned with this method.您不能期望使用此方法返回数据。
Refer this MSDN documentation.请参阅此 MSDN 文档。 https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationaldatabasefacadeextensions.executesqlinterpolated?view=efcore-3.1
https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationaldatabasefacadeextensions.executesqlinterpolated?view=efcore-3.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.