所以首先是我的 C# 代码,然后是存储过程。
public DataTable GetCourseHighPass(String tmpCourse)
{
command.Connection = OpenConnection();
try
{
command.CommandText = "exec GetCourseCompletions @tmpCourse = '" + tmpCourse + "'";
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dataTable);
return dataTable;
}
catch (Exception)
{
throw new Exception("There are no VG's for this course.");
}
finally
{
command.Connection.Close();
}
}
这是我的存储过程。
create procedure GetCourseCompletions
@tmpCourse nvarchar(30)
as
select (count(pnr) * 100 / (select count(pnr)
from HasStudied
where courseCode = @tmpCourse
and count(pnr) =)) as VGPrecentage
from HasStudied
where grade >= 5
and courseCode = @tmpCourse
go
问题是,如果没有学生通过高分,我将得到除以零的异常。寻找有关如何捕获此异常的建议,以使程序不会崩溃,或者更好地重写存储过程,使其一开始就不会出现异常。
谢谢您的帮助!