[英]If condition to check for DBNull.Value
I'm trying to check for DBNull.Value
but in this case reader["Preferences"]
returns {} in the immediate window (why?) 我正在尝试检查
DBNull.Value
但在这种情况下, reader["Preferences"]
在立即窗口中返回{}(为什么?)
so when (string)reader["Preferences"];
因此,当
(string)reader["Preferences"];
executes I get a type casting error 执行我得到类型转换错误
if (reader["Preferences"] == System.DBNull.Value)
{
preferences = (string)reader["Preferences"];
}
You are trying to cast only when the value is DBNull.Value
. 您正在尝试只投了,如果值是
DBNull.Value
。
You should invert your if
: 您应该将
if
反转:
if (reader["Preferences"] != System.DBNull.Value)
{
preferences = (string)reader["Preferences"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.