如果找到记录则返回int,如果未找到数据则返回false

Cav*_*rob 0 c# return-type

我有一种方法可以从学生的桌子上获得成绩.如果没有记录(null结果集),那么它应该返回false.

我是否编写了一个返回布尔值(找到,未找到)和整数作为参考参数的函数?

这是我到目前为止(如果找不到记录,我从proc返回-1)

public static int getParticipationGrade(SqlConnection sqlConn, int enrollmentID)
{
    SqlCommand sqlCmd = new SqlCommand("dbo.usp_participation_byEnrollmentID_Select", sqlConn);
    sqlCmd.CommandType = CommandType.StoredProcedure;
    sqlCmd.Parameters.AddWithValue("@enrollmentID", enrollmentID);

    int ret = 0;
    sqlConn.Open();
    ret = (int)sqlCmd.ExecuteScalar();
    sqlConn.Close();
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*car 8

我会返回int?其中null表示未找到.

public static int? getParticipationGrade(SqlConnection sqlConn, int enrollmentID)
{
    SqlCommand sqlCmd = new SqlCommand("dbo.usp_participation_byEnrollmentID_Select", sqlConn); 
    sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@enrollmentID", enrollmentID); 
    int ret = 0; 
    sqlConn.Open(); 
    ret = (int)sqlCmd.ExecuteScalar(); 
    sqlConn.Close(); 
    return ret < 0 ? (int?) null : ret;
} 
Run Code Online (Sandbox Code Playgroud)