当前位置:
首页 >
从泛型中检索数据
发布时间:2025/3/20
41
豆豆
从泛型中检索数据
在ITOO中有一个需求,根据用户的选择,查询考试信息,前台包含了四个控件,考试日期,考试时间,考试名称和考场名称。
问题在于这四个控件都不是必选的,要根据控件的内容是否为空,在后台进行查询用户想要的考试信息。
第一种解决方法
进行SQL语句的拼接,在后台查询数据库的时候,通过拼接SQL语句实现查询功能。
<span style="font-size:24px;">public List<vstudentexaminfoViewModel> QuerySelectStudentExamInfoView(string examDate, string startTime, string examName, string examPlace){//查询考试结束日期>日期examDate,考试结束时间>nowTime的考生为本场考试的所有考生List<vstudentexaminfoViewModel> list = new List<vstudentexaminfoViewModel>();StringBuilder StrSql = new StringBuilder();if (examDate == null){return list;}else {StrSql.Append("SELECT v.*,t.ExamCourseName FROM v_studentexaminfo v LEFT JOIN tr_coursemapname t ON v.CourseID = t.CourseID where ExamDate=@examDate ");if (startTime != null && startTime != "全选"){StrSql.Append("and StartTime=@startTime ");}if (examName != null && examName!="全选"){StrSql.Append("and ExamName=@examName ");}if (examPlace != null && examPlace != "全选"){StrSql.Append("and ExamPlace = @examPlace ");}StrSql.Append("GROUP BY v.StudentNo order by EndTime;"); } MySqlParameter[] paras = new MySqlParameter[] { new MySqlParameter("@examDate",examDate),new MySqlParameter("@startTime",startTime ),new MySqlParameter("@examName",examName),new MySqlParameter("@examPlace",examPlace )};DataTable dt = MySQLHelper.ExecuteDataTable(StrSql.ToString(), paras);List<vstudentexaminfoViewModel> listStudentExamInfo = TableToEntity<vstudentexaminfoViewModel>.ConvertToList(dt);return listStudentExamInfo;}#endregion</span>第二种解决方法
在泛型中检索数据,这里使用的EF框架,底层都是封装好的可以直接调用,直接在B层中写一个方法,这个方法是查询整个考试安排表,或者视图的,返回的是一个泛型。
在前台Veiw中进行判断,用户选择的条件是否为空,然后调用Controller中不同的方法,Controller中的这些方法都调用B层的同一个方法,然后在返回的泛型中,通过不同的查询条件检索数据。
B层的方法,返回泛型。
<span style="font-size:24px;">public List<v_examinformation> QueryExamInfo(string examDate){try{MySqlConnection conn = MySQLHelper.GetConnection;string sql = "select * from v_examinformation where ExamDate=@examDate";MySqlParameter[] paras = new MySqlParameter[] { new MySqlParameter("@examDate",examDate)};DataTable dt = MySQLHelper.ExecuteDataTable(sql, paras);List<v_examinformation> examinfomation = ModelConvertHelper<v_examinformation>.ConvertToModel(dt).ToList();return examinfomation;}catch (Exception){throw;}}</span>Controller中的方法,根据参数的不同,调用B层的同一个方法,从返回的泛型中检索数据。
<span style="font-size:24px;">public string QueryExamInfoNameTimePlace(){var date = Request.QueryString["date"];var name = Request.QueryString["name"];var time = Request.QueryString["time"];List<v_examinformation> examinfomation = new List<v_examinformation>();List<v_examinformation> list = new List<v_examinformation>();examinfomation = examinfomationViewSerivceBll.QueryExamInfo(date);for (int i = 1; i < examinfomation.Count; i++){if (examinfomation[i].ExamName == name && examinfomation[i].StartTime == time){list.Add(examinfomation[i]);}}JavaScriptSerializer serializer = new JavaScriptSerializer();string strJson = serializer.Serialize(list);return strJson;} </span>总结
- 上一篇: EasyUI的datebox用法
- 下一篇: EasyUI的combobox用法