public void SetMaxPaging(ForPaging Paging, string Search) { int Row = 0; string sql = $@"SELECT * FROM Guestbooks WHERE Name LIKE'%{Search}%' OR Content LIKE '%{Search}%' OR Reply LIKE '%{ Search}%'; "; try { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Row++; } } catch (Exception e) { throw new Exception(e.Message.ToString()); } finally { conn.Close(); } //計算總頁數 Paging.MaxPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Row) / Paging.ItemNum)); //重新設定正確頁數 Paging.SetRightPage(); }
//無搜尋值的搜尋資料方法 public IQueryable <Guestbooks> GetAllDataList(ForPaging Paging) { //宣告要回傳的搜尋資料為資料庫中的Guestbooks資料表 IQueryable <Guestbooks> Data = db.Guestbooks; //計算所需的總頁數 Paging.MaxPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Data.Count()) / Paging.ItemNum)); //重新設定正確的頁數,避免有不正確值傳入 Paging.SetRightPage(); //回傳搜尋資料 return(Data); }
//包含搜尋值的搜尋資料方法 public IQueryable <Guestbooks> GetAllDataList(ForPaging Paging, string Search) { //根據搜尋值來搜尋資料 IQueryable <Guestbooks> Data = db.Guestbooks .Where(p => p.Name.Contains(Search) || p.Content.Contains(Search) || p.Reply.Contains(Search)); //計算所需的總頁數 Paging.MaxPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Data.Count()) / Paging.ItemNum)); //重新設定正確的頁數,避免有不正確值傳入 Paging.SetRightPage(); //回傳搜尋資料 return(Data); }