从泛型中检索数据
從泛型中檢索數據
? ? ? ? ? 在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用法