【发布时间】: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