我使用DataReader读取the命令中的行。
我的问题是,我希望从我的数据库返回所有列,而错误是他在一列中找到了DBNull。
我该怎么办才能解决这个问题?
注意:返回空的列是string类型。
while(sqlDataReader.Read())
{
if (sqlDataReader.HasRows)
{
mylist.Add(new User()
{
Id = (int)sqlDataReader["Id"],
Name = (string)sqlDataReader["Name"],
File= (string)sqlDataReader["File"] <-- This is the one which contains some columns Null
});
}
}发布于 2013-09-27 09:20:50
使用来自DataReader的IsDBNull()方法。
if (sqlDataReader.HasRows)
{
while(sqlDataReader.Read())
{
if(!sqlDataReader.IsDBNull(1)) //pass the column index.
{
object value=sqlDataReader[1];
}
}
}发布于 2013-09-27 09:18:59
试一试
File=(sqlDataReader["File"] as string).GetValueOrDefault("");希望这能帮到你
参考GetValueOrDefault
https://stackoverflow.com/questions/19047038
复制相似问题