我对这个 try, catch 和最后的 return 语句工作流程毫无疑问......
此功能用于检索员工休假信息以供主管查看。它工作得很好,但是如果找到了 if 语句的数据,它将被返回,否则 else 块将被返回。即使两者都得到回报,它也会最终声明。我不知道为什么?
这里的代码片段:
List<Leave> ILeaveData.GetLeaveForSupervisorView(int userID)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("Storeprocedurename", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int));
cmd.Parameters["@Id"].Value = userID;
// Get employee leave information
try
{
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
List<Leave> leave = new List<Leave>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
leave.Add(CreateLeaveForAdminViewFromDataRow(ds.Tables[0].Rows[i]));
}
return leave; // if data found then statement return here
}
else
{
return null; // otherwise return here
// throw new Exception("Data Error");
}
}
catch (SqlException err)
{
IErrorLog elog = new ErrorLog(_connectionString);
elog.LogSystemError(err);
throw new ApplicationException("Data Error", (Exception)err);
}
finally
{
if (con != null)
{
con.Close();
}
}
}
关于萨尔瓦