EF/SQL/新闻中分页应用

1、EF分页

public IList<V_Test> GetTestPageLoad(int pagesize, int pageindex, out int total)

        {

            try

            {

                TestOnLineEntities db = new TestOnLineEntities();



                var testlist = from vtest in db.V_Test

                               orderby vtest.TestTime descending

                               select vtest;

                var result = testlist.ToList();               

                total = ViewTest.Count;

                //跳过的总条数

                int totalNum = (pageindex - 1) * pagesize;

                ViewTest = ViewTest.Skip(totalNum).ToList();

                if (ViewTest.Count > pagesize)

                {

                    ViewTest.RemoveRange(pagesize, total - pagesize);

                }

                return ViewTest;

            }

            catch (Exception)

            {

                total = 0;

                return null;

            }

        }

2、SQL语句分页

 /// <summary>

        /// 显示分页信息

        /// </summary>

        /// <param name="totleCount">总条数</param>

        /// <param name="pageNum">请求的是第几页</param>

        /// <param name="pageSize">每页显示条数</param>

        /// <param name="strType">信息类型</param>

        /// <returns></returns>

        public static DataSet PageListArticle(out int totleCount, int pageNum, int pageSize,string strType)

        {

            try

            {

                //sql语句实现分页功能

                string sqlstr = "select top " + pageSize + " * from Article" +

                                 "where (Article.type=" + strType + " and Article.articleId>(" +

                                 "select MAX(articleId) " +

                                 " from (select TOP " + pageSize * (pageNum - 1) + " articleId " +

                                 "from Article" +

                                 "order by articleId) as articleId ))" +

                                 "order by Article.articleId";

                DataSet result = db.GetTableBySql(sqlstr);

                //获取总记录数目

                totleCount = result.Tables[0].Rows.Count;

                if (result != null)

                    return result;

                else

                    return null;

            }

            catch (Exception)

            {

                totleCount = 0;

                return null;

            }

        }

3、新闻内容分页

思路:在新闻编辑时,在需要分页的地方插入分页符(如“【分页符】”),显示时,根据分页符的位置进行分页。

using System.Text;

//为内容生成分页

        public static string ContentPage(string content, int page)

        {

            string[] array_content = content.Split(new string[] { "【分页符】" }, StringSplitOptions.None);

            int length = array_content.Length;

            if (length <= 1)

            {

                return content;

            }

            return array_content[page - 1] + MakePage(length, page);

        }

        public static string MakePage(int length, int page)

        {

            string url;

            int ic = HttpContext.Current.Request.RawUrl.IndexOf("&page");

            if (ic > 0)

            {

                url = HttpContext.Current.Request.RawUrl.Remove(ic);

            }

            else

            {

                url = HttpContext.Current.Request.RawUrl;

            }

            StringBuilder builder = new StringBuilder();

            builder.Append("<center><table id=\"pagelist\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"text-decoration:none;\" align=\"center\" width=\"auto\">");

            builder.Append("<tr><td align=\"center\"><ul><li style=\"list-style-type:none;float:left;margin-left:5px;\"

><a href=\"" + url + "\">首页</a></li><li style=\"list-style-type:none;float:left;margin-left:5px;\"

><a href=\"" + url + "&page=" + (page - 1).ToString() + "\">上一页</a></li>");

            for (int i = 0; i < length; i++)

            {

                if (page - 1 == i)

                {

                    builder.Append("<li class=\"current\" style=\"list-style-type:none;float:left;margin-left:5px;\"

>" + (i + 1).ToString() + "</li>");

                }

                else

                {

                    builder.Append("<li style=\"list-style-type:none;float:left;margin-left:5px;\"

><a href=\"" + url + "&page=" + (i + 1).ToString() + "\">" + (i + 1).ToString() + "</a></li>");

                }

            }

            if (page < length)

            {

                builder.Append("<li style=\"list-style-type:none;float:left;margin-left:5px;\"

><a href=\"" + url + "&page=" + (page + 1).ToString() + "\">下一页</a></li>");

            }

            else

            {

                builder.Append("<li style=\"list-style-type:none;float:left;margin-left:5px;\"

><a href=\"" + url + "&page=" + page.ToString() + "\">下一页</a></li>");

            }

            builder.Append("<li style=\"list-style-type:none;float:left;margin-left:5px;\"

><a href=\"" + url + "&page=" + length.ToString() + "\">尾页</a></li></ul></td></tr></table></center>");            

            return builder.ToString();

        }

如图:
EF/SQL/新闻中分页应用

 

你可能感兴趣的:(sql)