通过sql 实现简单分页(not in)

 /// <summary>
    /// 分页查询的sql语句
    /// </summary>
    /// <param name="attributes">要查询的字段(两端可以没有空格)</param>
    /// <param name="pageSize">每页要显示的行数</param>
    /// <param name="pageIndex">当前页索引</param>
    /// <param name="orderBy">依据那个字段排序(两端可以没有空格)</param>
    /// <param name="aod">升序(asc)还是降序(desc)(两端可以没有空格)</param>
    /// <param name="where">附加的条件(一定要有where关键字,两端可以没有空格)</param>
    /// <returns></returns>
    public static string SelectByPaging(int currentPageIndex, string field, int pageSize, string strWhere, string orderByWho, string orderDirection)
    {
        string ids = string.Format("SELECT TOP({0}*{1}) id FROM Base_Knowledge {2} ORDER BY {3} {4}", currentPageIndex, pageSize, strWhere, orderByWho, orderDirection);
        string whereLess = null;
        if (!string.IsNullOrEmpty(strWhere))
        {
            int whereIndex = strWhere.ToLower().IndexOf("where");
            whereLess = "AND" + strWhere.Substring(whereIndex + 5);
        }
        return string.Format(@"SELECT TOP {0} {1} FROM Base_Knowledge WHERE id NOT IN({2}) {3} ORDER BY {4} {5}",
        pageSize, field, ids, whereLess, orderByWho, orderDirection);
    }
View Code
原文地址:https://www.cnblogs.com/zoumin123/p/6387425.html