简体   繁体   English

iSeries 中压缩十进制在 C# 中对应的数据类型是什么?

[英]What is the corresponding data type in C# for a packed decimal in iSeries?

Trying write a simple API to access a Db2 database table with packed decimal fields.尝试编写一个简单的 API 来访问带有压缩十进制字段的 Db2 数据库表。 Without converting the value, is there a corresponding data type in C#?不转换值,C#中是否有对应的数据类型? I keep getting a mismatching data type error.我不断收到不匹配的数据类型错误。

{"Specified cast is not valid."} Data: {System.Collections.ListDictionaryInternal} HResult: -2147467262 HelpLink: null InnerException: null Message: "Specified cast is not valid." {“指定的转换无效。”} 数据:{System.Collections.ListDictionaryInternal} HResult:-2147467262 HelpLink:null InnerException:null 消息:“指定的转换无效。” Source: "IBM.Data.DB2.Core" StackTrace: " at IBM.Data.DB2.Core.DB2DataWrapper.get_Int32Value()\\r\\n at IBM.Data.DB2.Core.DB2DataReader.GetInt32(Int32 i)\\r\\n at 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 at 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 1.MoveNext()\\r\\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n at System.Runtime.CompilerServices.Tas来源:“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 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at 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 at CoreCodeCamp.Controllers.TestController.d__4.MoveNext() in TestController.cs:line 38" TargetSite: {Int32 get_Int32Value()} 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()}

Here is the code...这是代码...

DB2 Camps table: 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: 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");
  }
}

DBcontext.cs:数据库上下文.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 Entity: 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; }
    }
}

Repository.cs:存储库.cs:

public async Task<Camp[]> GetAllCampsAsync(bool includeTalks = false)
{
  _logger.LogInformation($"Getting all Camps");

  IQueryable<Camp> query = _context.Camps;

  return await query.ToArrayAsync();
}

You should show your code...你应该显示你的代码...

But the IBM i .NET data provider should have a iDB2Decimal structure used for packed numeric.但是 IBM i .NET 数据提供程序应该有一个用于压缩数字的 iDB2Decimal 结构。

iDB2Decimal.Value returns a C# Decimal type. iDB2Decimal.Value 返回 C# Decimal 类型。

EDIT编辑
ok, so you have both packed decimal CAMPID P 5,0好的,所以你有两个压缩十进制CAMPID P 5,0
and zoned decimal LENGTH S 3,0并分区十进制LENGTH S 3,0

which should correspond to iDB2Decimal and iDB2Numeric.这应该对应于 iDB2Decimal 和 iDB2Numeric。

Both of which have a Value property that returns a C# Decimal.两者都具有返回 C# 十进制的 Value 属性。

So I'd recommend changing your model from int to Decimal to those two fields.所以我建议将您的模型从int更改为Decimal到这两个字段。
Location is also packed in the DB, I'm not sure what you've done with your Location type... LOCATIONID P 5,0位置也包含在数据库中,我不确定您对Location类型做了什么... LOCATIONID P 5,0
public Location Location { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM