if (string.IsNullOrEmpty(SortClause)) SortClause = PK;
int start_row_num = (PageIndex -1)*PageSize +1;
sb.AppendFormat(" from {0}", FromClause); if (WhereClause.Length >0) sb.AppendFormat(" where 1=1 {0}", WhereClause);
if (!string.IsNullOrEmpty(GroupClause)) sb.AppendFormat(" group by {0}", GroupClause);
string countSql =string.Format("Select count(0) {0};", sb); string tempSql = string.Format( "WITH t AS (SELECT ROW_NUMBER() OVER(ORDER BY {0}) as row_number,{1}{2}) Select * from t where row_number BETWEEN {3} and {4};", SortClause, SelectClause, sb, start_row_num, (start_row_num + PageSize -1));
if (string.IsNullOrEmpty(SortClause)) SortClause = PK;
int start_row_num = (PageIndex -1)*PageSize +1;
sb.AppendFormat(" from {0}", FromClause); if (WhereClause.Length >0) sb.AppendFormat(" where 1=1 {0}", WhereClause);
if (!string.IsNullOrEmpty(GroupClause)) sb.AppendFormat(" group by {0}", GroupClause);
return string.Format( "WITH t AS (SELECT ROW_NUMBER() OVER(ORDER BY {0}) as row_number,{1}{2}) Select * from t where row_number BETWEEN {3} and {4}", SortClause, SelectClause, sb, start_row_num, (start_row_num + PageSize -1)); } }
if (!string.IsNullOrEmpty(code)) { query.WhereClause.Append(" and ID= @ID"); }
a) GenerateCountSql ()方法生成的语句为: Select count(0) from TestTable Where 1=1 and ID= @ID
b) GenerateSql()方法生成的语句为: WITH t AS (SELECT ROW_NUMBER() OVER(ORDER BY ECID DESC) as row_number, * from TestTable where 1=1 and ID= @ID) Select * from t where row_number BETWEEN 1 and 20
c) GenerateSqlIncludetTotalRecords()方法生成的语句为: WITH t AS (SELECT ROW_NUMBER() OVER(ORDER BY E.ECID DESC) as row_number,* from TestTable where 1=1 and ID= @ID) Select * from t where row_number BETWEEN 1 and 20;Select count(0) from ECBasicInfo where 1=1 and ID= @ID;
注意:以上代码生成的SQL语句是曾对SQL SERVER 2005以上版本的,希望这些代码对大家有用