SqlConnection 和 SqlCommand 放在 try{} 外面 有什么好处???
public class GradeService{
public List<Grade> GetAllGrade()
{
List<Grade> gradeList =new List<Grade>();
SqlConnection sqlConn = null;
SqlCommand sqlComm = null;
try
{
string sql = string.Format("SELECT * FROM Grade");
sqlConn = DBHelper.GetSqlConn();
sqlComm = new SqlCommand(sql,sqlConn);
SqlDataReader sqlDR = sqlComm.ExecuteReader();
while(sqlDR.Read())
{
Grade grade = new Grade();
grade.GradeId =Convert.ToInt32(sqlDR["GradeID"].ToString());
grade.GradeName = sqlDR["GradeName"].ToString();
gradeList.Add(grade);
}
return gradeList;
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlConn.Close();
}
}
}