Bur*_*jua 7 c# asp.net null dbnull sqldatareader
我有一个包含空值的表,我需要使用SqlDataReader从表中获取数据.我无法弄清楚如何安全地将DBNull强制转换为int.
我现在正以这种方式做到这一点:
...
reader = command.ExecuteReader();
while (reader.Read()) {
int y = (reader["PublicationYear"] != null) ? Convert.ToInt32(reader["PublicationYear"]) : 0;
...
}
...
Run Code Online (Sandbox Code Playgroud)
但是Object cannot be cast from DBNull to other types.当PublicationYear为null时获取.
我怎样才能安全地获得价值?
谢谢.
| 归档时间: |
|
| 查看次数: |
14721 次 |
| 最近记录: |