空值 [英] NULLs
问题描述
嗨。
我有一个数据库,我正在将一些整数读入我的C#应用程序。
问题是这些DB值中的一些是NULL,所以当我尝试读取它们时,显然会抛出错误
。是否有一种表示NULL
值类型的标准方法?有人对此有任何提示吗?
Mike
Hi.
I have a database from which I am reading some integers into my C# app. The
problem is some of these DB values are NULL, so obviously an error is thrown
when I try to read them. Is there a standard way of representing a NULL
value type? Anyone got any tips on this?
Mike
推荐答案
嗨Mike,
检查DBNull(.Value)
-
快乐编码!
Morten Wennevik [C# MVP]
Hi Mike,
Check for DBNull(.Value)
--
Happy coding!
Morten Wennevik [C# MVP]
>嗨Mike,
检查DBNull(.Value)
Check for DBNull(.Value)
是的,我知道我可以先检查NULL,但是我希望能够在我的应用中代表这个NULL值。
。就像在数据库中一样,我可以将
INTEGER字段设置为NULL,但在C#中,我不能将int变量设置为NULL。人们如何解决这个问题?
Mike
Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can''t set an int variable to NULL. How
do people get around this?
Mike
好吧,对于正数,我会用-1。对于负数和正数...嗯......也许选择一个数字,任何数字并将其视为NULL,例如Int32.MinValue。
-
快乐的编码!
Morten Wennevik [C#MVP]
Well, for positive numbers, I would use -1. For both negative and positive numbers ... um ... maybe pick a number, any number and treat it as NULL, Int32.MinValue for instance.
--
Happy coding!
Morten Wennevik [C# MVP]
这篇关于空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!