在 C# 中从 DB.Null 转换为十进制的任何方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1115667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
any way to convert from DB.Null to decimal in C#
提问by priyanka.sarkar
Is there any way to convert from DB.Null to decimal in C#?
有没有办法在 C# 中从 DB.Null 转换为十进制?
Like
喜欢
Object[] returnvalue = new Object[1];
returnvalue[0] = null;
returnvalue[0] = Convert.ToDecimal(returnvalue[returnvalue.Length - 1]);
Though CLR is saying that DBNull.Value cannot be type casted. Still I want to know is there
尽管 CLR 说 DBNull.Value 不能进行类型转换。我还是想知道在那里
any work around? Thanks in advance
任何解决办法?提前致谢
采纳答案by Matthew Scharley
How do you expect to cast null
to anything? It's null, nothing, nada...
你如何期望投射null
到任何东西?它是空的,什么都没有,纳达……
That said, your best bet would be to check for DBNull
(or null
in the above case) and use/return/whatever default(decimal)
instead, though null
and DBNull
both have a significant semantic difference from just a default value; they are the absence of a value, so this is probably not what you want to do.
这就是说,你最好的选择将是检查DBNull
(或者null
在上述情况下),并使用/返回/不管default(decimal)
代替,但null
并DBNull
都有从仅仅是一种默认值的显著语义差别; 它们没有值,所以这可能不是您想要做的。
回答by Nippysaurus
If DBNull were to be converted to a Decimal, what value would you expect that decimal to have?
如果将 DBNull 转换为小数,您希望小数具有什么值?
I think there is a Decimal.TryParse() function which would fail more gracefully.
我认为有一个 Decimal.TryParse() 函数会更优雅地失败。
回答by Marc Gravell
There is a big difference between DBNull
and null
; your example only shows null
...
DBNull
和之间有很大的区别null
;你的例子只显示null
...
But in answer; not really... what value whould you give it?
但在回答; 不是真的......你给它什么价值?
You could test and use the default, or use Nullable<decimal>
(aka decimal?
):
您可以测试并使用默认值,或使用Nullable<decimal>
(又名decimal?
):
With null
(not DBNull
)
与null
(不是DBNull
)
decimal? valOrNull = (decimal?)returnvalue[0];
or to take DBNull
into account:
或DBNull
考虑:
object val = returnvalue[0];
decimal? valOrNull = (val == null || val is DBNull) ? (decimal?)null
: (decimal?)val; // or convert if it is a string etc
回答by Bevan
Have you considered using a nullable-decimal for your return value?
您是否考虑过对返回值使用可为空的十进制?
Though you'd still have to explicitly handle DB.Null, as that's different to null, you'd be able to differentiate the cases.
尽管您仍然必须显式处理 DB.Null,因为它与 null 不同,但您可以区分情况。
The code might look something like this ...
代码可能看起来像这样......
if (value == DB.Null)
return null;
else return decimal.Parse(value.ToString());
回答by Nikki9696
object foo = DBNull.Value;
decimal d = 0;
d = (decimal)(foo as decimal? ?? 0); // handles dbnull fine with default 0
MessageBox.Show(d.ToString());
foo = 1234M;
d = (decimal)(foo as decimal? ?? 0); // handles dbnull fine with default 0
MessageBox.Show(d.ToString());
回答by Null Head
Would this help?
这会有帮助吗?
returnvalue[returnvalue.Length - 1] is DBNull ? 0 : (decimal?)returnvalue[returnvalue.Length - 1] ?? 0
回答by fsbflavio
You can simply do this:
你可以简单地这样做:
Object[] returnvalue = new Object[1];
returnvalue[0] = DBNull.Value; //null from database
returnvalue[0] = returnvalue[0] == DBNull.Value
? null
: (Nullable<decimal>) Convert.ToDecimal(returnvalue[returnvalue.Length - 1]);
回答by Vishav Premlall
We use the below method to cast DBNull fields to decimal values in C#. The case where the field might be null can be catered for when giving the value of the field to the variable using a shorthand if.
我们使用以下方法将 DBNull 字段转换为 C# 中的十进制值。当使用速记 if 将字段的值赋予变量时,可以满足字段可能为 null 的情况。
cmd.Fill<Entity>(result, a => new Entity()
{
EntityProperty = (decimal)(a["FieldName"] == DBNull.Value ? null : a["FieldName"]),
});