【问题标题】:How to call a stored procedure with JSON output in EF Core 5如何在 EF Core 5 中调用带有 JSON 输出的存储过程
【发布时间】:2021-12-27 20:45:22
【问题描述】:

我有一个带有 JSON 格式输出的存储过程。你知道 JSON 格式的输出是字符串格式的。

数据库上下文对象只返回表类型。

前:

_context.tableA.FromSqlRaw()

但是我的场景除了字符串没有任何类型。

如何使用字符串输出调用此存储过程并将结果呈现给客户端?

【问题讨论】:

    标签: c# .net-core asp.net-core-mvc ef-core-5.0


    【解决方案1】:

    你必须使用输出参数

    var output = new SqlParameter();
    output.ParameterName = "@jsonOutput";
    output.SqlDbType =  SqlDbType.NVarChar;
    output.Direction = ParameterDirection.Output;
    
    var param1 = new SqlParameter();
    output.ParameterName = "@ID";
    output.SqlDbType = SqlDbType.Int;
    
    
    await _context.Database.ExecuteSqlRawAsync(
    "EXEC dbo.SpName @ID={0} @jsonOutput={1} OUT", param1,output);
    
    var json =output.Value;
    

    存储过程

    ...
    SET @jsonOutput = (SELECT ....
        FROM ...
        FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
    ...
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多