[英]Is Java's BigDecimal the closest data type corresponding to C#'s Decimal?
[英]What is the corresponding data type in C# for a packed decimal in iSeries?
尝试编写一个简单的 API 来访问带有压缩十进制字段的 Db2 数据库表。 不转换值,C#中是否有对应的数据类型? 我不断收到不匹配的数据类型错误。
{“指定的转换无效。”} 数据:{System.Collections.ListDictionaryInternal} HResult:-2147467262 HelpLink:null InnerException:null 消息:“指定的转换无效。” 来源:“IBM.Data.DB2.Core” StackTrace:“在 IBM.Data.DB2.Core.DB2DataWrapper.get_Int32Value()\\r\\n 在 IBM.Data.DB2.Core.DB2DataReader.GetInt32(Int32 i)\\r \\n 在 Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable 1.AsyncEnumerator.<MoveNextAsync>d__17.MoveNext()\\r\\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.ValueTaskAwaiter
1.GetResult()\\r\\n 在 Microsoft。 EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.d__22 1.MoveNext()\\r\\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__22
d__2Next()1.MoveNext() \\r\\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n 在 System.Runtime.CompilerServices.Tas kAw aiter.ThrowForNonSuccess(Task task)\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n 在 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\\r\\n at CoreCodeCamp.Data.TestRepository.<GetAccountAsync>d__6.MoveNext() in TestRepository.cs:line 60\\r\\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\\r\\n 在 CoreCodeCamp.Controllers.TestController.d__4 .MoveNext() in TestController.cs:line 38" TargetSite: {Int32 get_Int32Value()}
这是代码...
DB2 Camps 表:
Field Type Length
CAMPID P 5,0
NAME A 50
MONIKER A 20
EVENTDATE Z 26
LENGTH S 3,0
LOCATIONID P 5,0
CampsController.cs:
[HttpGet]
public async Task<ActionResult<CampModel[]>> Get(bool includeTalks = false)
{
try
{
var results = await _repository.GetAllCampsAsync(includeTalks);
return _mapper.Map<CampModel[]>(results);
}
catch (Exception)
{
return this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure");
}
}
数据库上下文.cs:
using IBM.EntityFrameworkCore;
using IBM.EntityFrameworkCore.Storage.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseDb2(_config.GetConnectionString("CodeCamp"),
p =>
{
p.SetServerInfo(IBMDBServerType.AS400, IBMDBServerVersion.AS400_07_01);
p.UseRowNumberForPaging();
p.MaxBatchSize(1);
});
}
Camp.cs 实体:
namespace CoreCodeCamp.Data
{
public class Camp
{
public int CampId { get; set; }
public string Name { get; set; }
public string Moniker { get; set; }
public Location Location { get; set; }
public DateTime EventDate { get; set; } = DateTime.MinValue;
public int Length { get; set; } = 1;
public ICollection<Talk> Talks { get; set; }
}
}
存储库.cs:
public async Task<Camp[]> GetAllCampsAsync(bool includeTalks = false)
{
_logger.LogInformation($"Getting all Camps");
IQueryable<Camp> query = _context.Camps;
return await query.ToArrayAsync();
}
你应该显示你的代码...
但是 IBM i .NET 数据提供程序应该有一个用于压缩数字的 iDB2Decimal 结构。
iDB2Decimal.Value 返回 C# Decimal 类型。
编辑
好的,所以你有两个压缩十进制CAMPID P 5,0
并分区十进制LENGTH S 3,0
这应该对应于 iDB2Decimal 和 iDB2Numeric。
两者都具有返回 C# 十进制的 Value 属性。
所以我建议将您的模型从int
更改为Decimal
到这两个字段。
位置也包含在数据库中,我不确定您对Location
类型做了什么... LOCATIONID P 5,0
public Location Location { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.