using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using Microsoft.Practices.EnterpriseLibrary.Data; using System.Globalization; using System.Data; namespace DSWeb.Areas.CommMng.DAL { public class PagerHelper { /// /// 将原始SQL语句修改为分页SQL语句 /// ※只针对未进行SQL分页的SQL语句使用! /// ※原始语句中必须存在ORDER BY 排序! /// ※ORDER BY字段不可使用别名 /// ※只适用于SQL语句中所有关键字为大写的情况下使用! /// ※只适用于where条件中不存在子查询的语句,例(WHERE a.id in (SELECT id FROM b))为不可用。 /// 若必须在where条件中添加子查询,请将子查询的FROM写成小写,例(WHERE a.id in (SELECT id from b))为可用。 /// /// 原SQL语句 /// 开始位置 /// 每页条数 /// 总页数 /// 新的分页SQL语句 public static StringBuilder PageSQL ( string OriginalSQLStr, int start, int limit,out int total) { //最后一个orderby语句位置 CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo; int orderByIndex = Compare.LastIndexOf(OriginalSQLStr, "order by", CompareOptions.IgnoreCase); //最后一个orderby语句 string orderByStatments = ""; //主查询语句 string mainSql = OriginalSQLStr; if (orderByIndex>0) { orderByStatments = OriginalSQLStr.Substring(orderByIndex, OriginalSQLStr.Length - orderByIndex); // 判断最后一个orderby语句是否在某个子查询里 // 如果是,则取消新查询语句的orderby拼凑 // 如果否,则删除原SQL语句中的orderby语句,等待拼凑新orderby语句 if (orderByStatments.IndexOf(")") > 0) { orderByStatments = ""; } else { mainSql = OriginalSQLStr.Substring(0, orderByIndex); } } //更改主查询语句的select部分以及orderby语句 string numColumn = " row_number() over ("+orderByStatments+") as num,"; mainSql = mainSql.Insert(7,numColumn); //创建新SQL主体 StringBuilder sb = new StringBuilder(); sb.Append("select * from ("); sb.Append(mainSql); sb.Append(") as t "); int endindex = start+limit; sb.Append("where t.num>" + start + " and t.num<=" + endindex+" order by t.num"); //总行数 total = getTotelForSqlNew(OriginalSQLStr); return sb; } public static int getTotelForSql (string OriginalSQLStr) { //最后一个orderby语句位置 CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo; int orderByIndex = Compare.LastIndexOf(OriginalSQLStr, "order by", CompareOptions.IgnoreCase); //最后一个orderby语句 string orderByStatments = ""; //主查询语句 string mainSql = OriginalSQLStr; if (orderByIndex > 0) { orderByStatments = OriginalSQLStr.Substring(orderByIndex, OriginalSQLStr.Length - orderByIndex); // 判断最后一个orderby语句是否在某个子查询里 // 如果是,则取消新查询语句的orderby拼凑 // 如果否,则删除原SQL语句中的orderby语句,等待拼凑新orderby语句 if (!(orderByStatments.IndexOf(")") > 0)) { mainSql = OriginalSQLStr.Substring(0, orderByIndex); } } //修改SELECT部分 //获取最后一个FROM部分 int fromIndex = Compare.LastIndexOf(mainSql,"FROM",CompareOptions.None); string newSql = mainSql.Remove(7, fromIndex - 7); newSql = newSql.Insert(7, " count(*) "); Database db = DatabaseFactory.CreateDatabase(); int totel=0; using (IDataReader reader = db.ExecuteReader(CommandType.Text, newSql)) { while (reader.Read()) { totel = Convert.ToInt32(reader[0]); } } return totel; } public static int getTotelForSqlNew(string OriginalSQLStr) { //最后一个orderby语句位置 CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo; int orderByIndex = Compare.LastIndexOf(OriginalSQLStr, "order by", CompareOptions.IgnoreCase); //最后一个orderby语句 string orderByStatments = ""; //主查询语句 string mainSql = OriginalSQLStr; if (orderByIndex > 0) { orderByStatments = OriginalSQLStr.Substring(orderByIndex, OriginalSQLStr.Length - orderByIndex); // 判断最后一个orderby语句是否在某个子查询里 // 如果是,则取消新查询语句的orderby拼凑 // 如果否,则删除原SQL语句中的orderby语句,等待拼凑新orderby语句 if (!(orderByStatments.IndexOf(")") > 0)) { mainSql = OriginalSQLStr.Substring(0, orderByIndex); } } string newSql = mainSql.Insert(0, "select COUNT(*) from ( "); newSql = newSql + " ) t1 "; Database db = DatabaseFactory.CreateDatabase(); int totel = Convert.ToInt32(db.ExecuteScalar(CommandType.Text,newSql)); return totel; } } }