gpt4 book ai didi

c# - 如何处理 FileHelper 类中 SQLDecimal 数据类型的空值

转载 作者:太空狗 更新时间:2023-10-29 21:07:20 24 4
gpt4 key购买 nike

我正在尝试使用 FileHelpers 加载一个文件(除了这个问题之外已经很喜欢了 :P )我必须将 CSV 文件数据保存到数据库中,因此我使用 SqlDecimal 数据类型来存储 CSV 文件的十进制值。

[FileHelpers.FieldOptional()]
[FileHelpers.FieldConverter(typeof(SqlDecimalConverter))]
public SqlDecimal Rate;

所有这些工作正常,直到我为 FixRate1 设置了空白值。这被标记为错误。

"Warn Exception:Null Value found for the field 'Rate' in the class 'SWTrade'. You must specify a FieldNullValueAttribute because this is a ValueType and can´t be null."

我尝试放置 [FileHelpers.FieldNullValue(SqlDecimal.Null)] 但它显然会引发错误。

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

即使我覆盖了 SqlDecimalConverter 类中的 FieldToString 方法,在读取数据时也不会调用该函数。

那么,既然如此,有没有一种方法可以将任何空值或什至一些其他硬编码值分配给 Rate 数据,然后我可以在我自己的逻辑中直接将其替换为空值?

如果您需要更多详细信息,请告诉我。提前致谢。

最佳答案

其他评论是对的,你需要使用

private decimal? internalRate;

然后为转换创建一个属性,实际上库只自动转换 native 类型而 SqlDecimal 不是。

public SqlDecimal? Rate
{
get
{
if (internalRate.HasValue)
return new SqlDecimal(internalRate.Value);
else
return null;
}
}

PS:如果您想使用自定义转换器方式,要将其用于读取,您需要重写方法StringToField(调用FieldToString 进行写入)

希望这有帮助:)

关于c# - 如何处理 FileHelper 类中 SQLDecimal 数据类型的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2011150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com