如何安全地将可空结果从sqlreader转换为int?
我有一个包含空值的表,我需要使用SqlDataReader从表中获取数据。 我无法弄清楚如何安全地将DBNull转换为int。
我现在正以这种方式做到这一点:
... reader = command.ExecuteReader(); while (reader.Read()) { int y = (reader["PublicationYear"] != null) ? Convert.ToInt32(reader["PublicationYear"]) : 0; ... } ...
但是获取Object cannot be cast from DBNull to other types.
当PublicationYear为null时。
我怎样才能安全地获得价值?
谢谢。
您应该将reader["PublicationYear"]
与DBNull.Value
进行比较,而不是null
。
DBNull
与null
。 你应该尝试这样的事情:
int y = (reader["PublicationYear"] != DBNull.Value) ? ...
您应该显式检查返回的值是否为DBNull
类型
while (reader.Read()) { int y = (!reader["PublicationYear"] is DBNull) ? Convert.ToInt32(reader["PublicationYear"]) : 0; ... }
实际上,您可以按值和类型进行此比较:
reader["PublicationYear"] != DBNull.Value
简而言之 – 您可以期望从数据库返回空值的DBNull
,而不是null本身。
作为替代方案,您可以执行以下操作。 当您将DBNull转换为0时,请更改执行select的过程。 这样select选项本身为null值返回零。
片段来展示这个想法
SELECT ... ,ISNULL (PublicationYear, 0) as PublicationYear ... FROM sometable
这样做的好处是,代码中不需要额外的检查。
更改
reader["PublicationYear"] != null
至
reader["PublicationYear"] != DBNull.Value
这是错误: (reader["PublicationYear"] != null)
你应该测试DBNull.Value ….
int ord = reader.GetOrdinal("PublicationYear"); int y = reader.IsDBNull(ord) ? 0 : reader.GetInt32(ord);
或者,或者:
object obj = reader["PublicationYear"]; int y = Convert.IsDBNull(obj) ? 0 : (int)obj;
将您的测试从(reader["PublicationYear"] != null)
更改为(reader["PublicationYear"] != DBNull.Value)
。
应将数据库空值与DBNull.Value进行比较
reader = command.ExecuteReader(); while (reader.Read()) { int y = (reader["PublicationYear"] != DBNull.Value) ? Convert.ToInt32(reader["PublicationYear"]) : 0; ... }