Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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# 我有一个错误对象不能从DBNull转换为其他类型?_C#_C# 4.0_C# 3.0_C# To Vb.net - Fatal编程技术网

C# 我有一个错误对象不能从DBNull转换为其他类型?

C# 我有一个错误对象不能从DBNull转换为其他类型?,c#,c#-4.0,c#-3.0,c#-to-vb.net,C#,C# 4.0,C# 3.0,C# To Vb.net,我的代码是: 当我做断点时,我正在从数据库中检索数据。它在列表中显示数据,但它也给了我一个错误 public static List<StudentScore> GetAllScore() { SqlConnection conn = MyDB.GetConnection(); string selectStm = "SELECT en.CourseID,en.Score,s.StudentID FROM EnrollmentTable en

我的代码是: 当我做断点时,我正在从数据库中检索数据。它在列表中显示数据,但它也给了我一个错误

    public static List<StudentScore> GetAllScore()
   {
       SqlConnection conn = MyDB.GetConnection();
       string selectStm = "SELECT en.CourseID,en.Score,s.StudentID FROM EnrollmentTable en,Student s WHERE en.StudentID = s.StudentID";
       SqlCommand command = new SqlCommand(selectStm, conn);
       List<StudentScore> aStudentScore = new List<StudentScore>();
       try
       {
           conn.Open();
           SqlDataReader reader = command.ExecuteReader();          
           Console.WriteLine(reader.HasRows.ToString());
           while (reader.Read())
           {
               StudentTable st = new StudentTable();
               CourseTable cr = new CourseTable();
               Enrollment enr = new Enrollment();
               StudentScore score = new StudentScore();
               enr.CourseData = cr;
               enr.StudentData = st;                                    
                   //score.EnrollmentData.StudentData.StudentID = reader["StudentID"].ToString();
                   //score.EnrollmentData.CourseData.CourseID = reader["CourseID"].ToString();                  
                   st.StudentID = reader["StudentID"].ToString();
               cr.CourseID = reader["CourseID"].ToString();
               score.Score = Convert.ToInt32(reader["Score"]);
               score.EnrollmentData = enr; 
               aStudentScore.Add(score);
           }
           reader.Close();
           return aStudentScore;
       }
       catch (SqlException ex)
       {
           throw ex;
       }
       finally
       {
           conn.Close();
       }

   }


}
}

它从数据库获取数据,但显示此错误…..无法将对象从DBNull强制转换为其他类型,因此,请告诉我如何修复它?

这意味着数据库中有空值。您必须在代码中检查它,或者将列架构更改为NOTNULL


现在必须处理C对象中的null值。

需要检查读取器是否为DBNULL类型

在尝试转换列之前,在读卡器上调用IsDBNull检查列:

using (reader = server.ExecuteReader(CommandType.Text, TopIDQuery, paramet))
{
   while (reader.Read())
   {
       var column = reader.GetOrdinal("TopID");

       if (!reader.IsDBNull(column))
          topID = Convert.ToInt32(reader[column]);
       }
   }
}
或者,与DBNull.Value进行比较:

var value = reader["TopID"];

if (value != DBNull.Value)
{
    topID = Convert.ToInt32(value);
}

DBNull用于表示数据库中的空值

在强制转换ir之前,应该检查该值是否为DBNull

object score = reader["Score"];

score.Score = score == DBNull.Value ? 0 : Convert.ToInt32(score);

哪一行代码引发了错误?您不能==DBNull-这是一种类型。你大概是说DBNull.Value。
object score = reader["Score"];

score.Score = score == DBNull.Value ? 0 : Convert.ToInt32(score);