简体   繁体   English

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

[英]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我找到了其他适合我的方法

  1. Add Nuget package Microsoft.Data.SqlClient添加 Nuget 包 Microsoft.Data.SqlClient

  2. 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.

相关问题 如何将多个参数传递给 .net core 3.1 上的存储过程 - How to pass multiple parameters to stored procedure on .net core 3.1 ExecuteSqlInterpolated/ExecuteSqlRaw 参数在 asp.net 内核中错误地传递到数据库 - ExecuteSqlInterpolated/ExecuteSqlRaw parameters pass to database incorrectly in asp.net core 如何使用EF执行带有输入和输出参数的存储过程? - How to execute stored procedure with input and output parameters using EF? 点网核心中的存储过程 - Stored Procedure in dot net core "如何编写参数为字符串列表的存储过程 [ SQL Server,ASP.NET Core 3.1 ]" - How to write a stored procedure in which the parameters are a list of strings [ SQL Server, ASP.NET Core 3.1 ] 如何从存储过程 EF Core 3.1 中获取多个 OUTPUT 参数 - How to get Multiple OUTPUT parameters from stored procedure EF Core 3.1 使用 EF Core 6 在 ASP.NET Core 6 中使用 FromSqlInterpolated 执行存储过程时出错 - Error executing stored procedure with FromSqlInterpolated in ASP.NET Core 6 with EF Core 6 如何使用.net core 3.1 SignalR用于存储过程 - How to use .net core 3.1 SignalR uses for stored procedure 使用 ASP.NET 核心和实体框架执行存储过程? - Execute stored procedure using ASP.NET Core and Entity Framework? 如何在 ASP.NET Core MVC 中使用 ADO.NET 将参数添加到存储过程? - How can I add parameters to a stored procedure using ADO.NET in ASP.NET Core MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM