Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataReader返回DBNULL_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# DataReader返回DBNULL

C# DataReader返回DBNULL,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在使用DataReader从sqlcommand读取行 我的问题是,我希望返回数据库中的所有列 错误是他在一列中找到了DBNull 我怎样才能解决这个问题 注意:返回Null的列是字符串类型 while(sqlDataReader.Read()) { if (sqlDataReader.HasRows) { mylist.Add(new User() { Id = (int)sqlDataReader["Id"],

我正在使用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
        });
    }
}
while(sqlDataReader.Read())
{
if(sqlDataReader.HasRows)
{
添加(新用户()
{
Id=(int)sqlDataReader[“Id”],
名称=(字符串)sqlDataReader[“名称”],
File=(字符串)sqlDataReader[“File”]试试看

希望这能对你有所帮助

请参阅尝试

希望这能对你有所帮助

请参阅使用DataReader中的IsDBNull()方法

if (sqlDataReader.HasRows)
{
  while(sqlDataReader.Read())
  {
    if(!sqlDataReader.IsDBNull(1)) //pass the column index.
    {
        object value=sqlDataReader[1];
    }

  }
 }
使用DataReader中的IsDBNull()方法

if (sqlDataReader.HasRows)
{
  while(sqlDataReader.Read())
  {
    if(!sqlDataReader.IsDBNull(1)) //pass the column index.
    {
        object value=sqlDataReader[1];
    }

  }
 }

我无法选择扩展GetValueorDefault,但“CreativeManix”的答案已经解决了它。但无论如何,感谢您的帮助。非常感谢。+1我无法选择扩展GetValueorDefault,但“CreativeManix”的答案已经解决了它。但无论如何,感谢您的帮助。。非常感谢。+1