此查询(方法 CalculateCGPA 中的命令)在 mysql Workbench 中运行良好。它仅返回包含 2.00 的 cgpa 的 1 行和 1 列。我想将它存储在我的 cgpa 字符串中,但它在这一行出现错误:cgpa = Convert.ToString(myReader.GetString(0));
错误说:“数据为 Null。不能对 Null 值调用此方法或属性。”
请注意,在进入程序上方的错误行之前,已经通过了 IF 语句测试,表明 READER 不为空。
有人可以帮助我吗?
public string GetStudentID()
{
List<string> list = new List<string>();
conn.Open();
string STDid = "SELECT Student_ID from Student";
MySqlCommand cmd = new MySqlCommand(STDid, conn);
MySqlDataReader myReader;
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
list.Add(Convert.ToString(myReader.GetString(0)));
}
myReader.Close();
conn.Close();
string[] array = list.ToArray();
string newcgpa = CalculateCGPA(array[0]);
return newcgpa;
}
public string CalculateCGPA(string arrayElement)
{
string cgpa;
conn.Open();
string command = "SELECT ROUND((sum(enroll.Enroll_Grade_Point*course.Crs_Credits) /sum(course.Crs_Credits)),2) as CGPA FROM student, enroll, course, class Where student.Student_ID = enroll.Student_ID AND enroll.Class_Code = class.Class_Code and class.Crs_Code = course.Crs_Code and student.Student_ID = '" + arrayElement + "'";
MySqlCommand cmd = new MySqlCommand(command, conn);
MySqlDataReader myReader;
myReader = cmd.ExecuteReader();
if (myReader.Read())
{
cgpa = Convert.ToString(myReader.GetString(0));
cmd.Connection.Close();
myReader.Close();
return cgpa;
}
else
{
cmd.Connection.Close();
myReader.Close();
return null;
}