最初に私の 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
問題は、ハイパスの生徒がいない場合、ゼロ除算の例外が発生することです。プログラムがクラッシュしないようにこの例外をキャッチする方法、またはそもそも例外が発生しないようにストアド プロシージャを書き直す方法についての提案を探しています。
ご協力いただきありがとうございます!