zoukankan      html  css  js  c++  java
  • 不用存储实现的分页,效率和存储过程一样。

    /// <summary>
          /// 获取分页操作SQL语句(对于排序的字段必须建立索引)
          /// </summary>
          /// <param name="tblName">操作表名</param>
          /// <param name="fldName">操作索引字段名称</param>
          /// <param name="PageIndex">当前页</param>
          /// <param name="PageSize">每页显示记录数</param>
          /// <param name="rtnFields">返回字段集合,中间用逗号格开。返回全部用“*”</param>
          /// <param name="OrderType">排序方式,0升序,1为降序</param>
          /// <param name="strWhere">检索的条件语句,不需要再加WHERE关键字</param>
          /// <returns></returns>
          public static string ConstructSplitSQL(string tblName, 
                                        string fldName, 
                                        int PageIndex, 
                                        int PageSize, 
                                        string rtnFields, 
                                        int OrderType, 
                                        string strWhere)
          {
             string strSQL = "";
             string strOldWhere = "";

             // 构造检索条件语句字符串
             if( strWhere != "" )
             {
                strOldWhere = " AND " + strWhere + " ";

                strWhere = " WHERE " + strWhere + " ";
                // 去除不合法的字符,防止SQL注入式攻击
                strWhere = strWhere.Replace("'", "''");
                strWhere = strWhere.Replace("--", "");
                strWhere = strWhere.Replace(";", "");
             }

             // 升序操作
             if( OrderType == 0 )
             {
                if( PageIndex == 1 )
                {
                   strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
                   strSQL += strWhere + "ORDER BY " + fldName + " ASC";
                }
                else
                {
                   strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";

                   strSQL += "WHERE (" + fldName + " > ( SELECT MAX(" + fldName + ") FROM (SELECT TOP " + ((PageIndex - 1)*PageSize) + " " + fldName + " FROM " + tblName + strWhere + " ORDER BY " + fldName + " ASC ) AS T )) ";
                
                   strSQL += strOldWhere + "ORDER BY " + fldName + " ASC";
                }
             }
             // 降序操作
             else if( OrderType == 1 )
             {
                if( PageIndex == 1 )
                {
                   strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";
                   strSQL += strWhere + "ORDER BY " + fldName + " DESC";
                }
                else
                {
                   strSQL += "SELECT TOP " + PageSize + " " + rtnFields + " FROM " + tblName + " ";

                   strSQL += "WHERE (" + fldName + " < ( SELECT MIN(" + fldName + ") FROM (SELECT TOP " + ((PageIndex - 1)*PageSize) + " " + fldName + " FROM " + tblName + strWhere + " ORDER BY " + fldName + " DESC ) AS T )) ";
                
                   strSQL += strOldWhere + "ORDER BY " + fldName + " DESC";
                }
             }
             else // 异常处理
             {
                throw new DataException("未指定任何排序类型。0升序,1为降序");
             }

             return strSQL;
          }

    调用代码:cs文件的,参考。。


    DataProvider dp = null;
             user.dataType = "SqlClient";
             user.connectionString = "server=(local); uid=sa; pwd=1016; database=skyBoard";

             dp = user.InstanceDataProvider();

             string strCmd = "SELECT Count(ID) FROM [address]";

             int totalRecord = 1198954;
             int PageSize = 20;
             int PageIndex = (Request.QueryString["page"] == null) ? 1 : int.Parse(Request.QueryString["page"]);

             int pageNum = 0;

             if(totalRecord % PageSize == 0)
             {
                pageNum = totalRecord/PageSize;
             }
             else
             {
                pageNum = (totalRecord/PageSize) + 1;
             }

             //strCmd = String.Format(Seaskyer.FSO.FObject.ReadFile(@"E:\\a.txt"), PageSize * (PageIndex - 1) + 1);
             strCmd = Seaskyer.Strings.Function.ConstructSplitSQL("address", "ID", PageIndex, PageSize, "*", 1, "");


             Response.Write(strCmd);
             DataTable dt = dp.DataTableSQL(strCmd);

             Response.Write("共有" + totalRecord + ", " + user.SplitPages("test.aspx?", PageIndex, pageNum, totalRecord ));

             
             DataGrid1.DataSource = dt.DefaultView;
             DataGrid1.DataBind();

             dt.Clear();
             dt.Dispose();
  • 相关阅读:
    Windows Phone开发(29):隔离存储C 转:http://blog.csdn.net/tcjiaan/article/details/7447469
    Windows Phone开发(25):启动器与选择器之WebBrowserTask 转:http://blog.csdn.net/tcjiaan/article/details/7404770
    内存知识集
    牛人榜
    如何解决SQL Server 2000 中的连接问题(邹建)
    索引
    .net事件机制
    内核对象
    使用socket tcp实现通讯
    sql技巧
  • 原文地址:https://www.cnblogs.com/wangergo/p/256265.html
Copyright © 2011-2022 走看看