【问题标题】:Losing headers received from 4.7.2 API Dataset/datatable in dotnet Core 3 API丢失从 dotnet Core 3 API 中的 4.7.2 API 数据集/数据表接收的标头
【发布时间】:2020-05-14 21:02:09
【问题描述】:

我有 2 个 API,其中一个是用于 Sybase 报告的数据访问代理。这是在 Framework 4.7.2 中编写的,并返回一个 DTO,其中包含一个属性,该属性是由 DbFactory 系统填充的 Dataset,或者至少是它们周围的包装器。

除了当数据表为“空”时,其他 API 可以正常接收 DTO,因为只有标题但没有行,我只收到数据表的名称,但没有收到任何标题,因为我将其解析为尝试写入工作表时,Excel 工作表会出现“列超出范围”异常的问题。

我只找到了有限的信息,没有太多的解决方案。有人说 .Net Standard 2+ 应该解决 Framework 和 Core 序列化数据集的兼容性问题。

我需要一种方法来可靠地从此 API 获取数据表/数据集,以便在没有行时返回仅包含标题的 Excel 工作簿。

using (connection)
        {
            try
            {
                connection.ChangeDatabase(schema.DatabaseName);
                command.CommandText = $"{schema.Name}.{spName}";
                command.CommandType = CommandType.StoredProcedure;

                if (pList != null)
                {
                    for (int i = 0; i < pList.Count; i++)
                    {
                        command.Parameters.Add(GetParameter(pList[i].DefaultValue, pList[i].Name, pList[i].BaseParameter?.DbTypeName));
                    }
                }
                command.Fill(ds);

                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[i].TableName = $"{spName}{i + 1}";
                }
            }
            catch (Exception e)
            {
                ErrorLogger.Error($"{e.Message}, {e.InnerException} with a trace of {e.StackTrace}", e);
                if(e.Source.Contains("SQL Anywhere .NET Data Provider"))
                    throw new Exception($"An error has occurred that has to do with the Stored Procedure and needs correcting at that level: {e.Message}");
                throw e;
            }
        }

我只是像这样在核心端进行标准反序列化:

report = await response.Content.ReadAsAsync<ResponseObject>();

更新: 因此,在 4.7.2 API 中,该对象似乎包含数据表中的元数据并显示标题,但是当我返回 Postman 时,它只是有一个命名数组。

"Table": {
        "ReportHighRiskEntityAccounts1": []
    },

如何确保 Datatable 元数据在从 4.7.2 API 传输时被序列化?

【问题讨论】:

    标签: c# .net api asp.net-core


    【解决方案1】:

    因此,我必须实际将一行强制放入空的 DataTable 中,以便序列化能够识别在这种情况下存在结构。

    我希望还有其他一些东西可以为以后的实现提供更优雅的解决方案,但这就是我现在所拥有的。

    if (dt.Rows.Count == 0)
    {
        clone.Rows.Add("No data for the parameters provided.");
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 2012-11-16
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多