我使用DataReader从我的sqlcommand读取行。
我的问题是,我想返回所有列从我的数据库和错误是,他发现DBNull在一列。
我该怎么做才能解决这个问题呢?
注意:返回Null的列是字符串类型。
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
});
}
}
字符串
2条答案
按热度按时间lb3vh1jj1#
使用DataReader中的 IsDBNull() 方法。
字符串
bihw5rsg2#
试试看:
字符串
请参阅GetValueOrDefault的文档。