/// <summary>
/// 根据查询条件获取指定表中的行数
/// </summary>
/// <param name="tableName"></param>
/// <param name="pageSize"></param>
/// <param name="whereList"></param>
/// <param name="RowCount"></param>
/// <returns></returns>
public static int GetRowCount(int pageSize, out int itemCount, string tableName, System.Collections.Generic.Dictionary<string, string> whereList)
{
StringBuilder strSql = new StringBuilder();
strSql.AppendFormat("select count(1) from {0} where 1=1 ", tableName);
if (whereList != null && whereList.Count > 0)
{
foreach (System.Collections.Generic.KeyValuePair<string, string> w in whereList)
{
if (w.Key == "SQL")
{
strSql.Append(w.Value);
}
else if (w.Key == "ORDER" || w.Key == "SORT")
continue;
else
{
strSql.AppendFormat(" and {0}='{1}'", w.Key, w.Value);
}
}
}
itemCount = int.Parse(DbHelperMySQL.GetSingle(strSql.ToString()).ToString());
if (pageSize == 0 || itemCount == 0) return 0;
if (itemCount % pageSize == 0)
return itemCount / pageSize;
else
return itemCount / pageSize + 1;
}
/// <summary>
/// Oracle分页方法
/// </summary>
/// <param name="currentPage">当前页</param>
/// <param name="pageSize">每页显示行数</param>
/// <param name="itemCount">返回总行数</param>
/// <param name="tableName">要获取的表</param>
/// <param name="whereList">Dictionary查询条,key,value键值形式(排序字段key: ORDER,升序降序 key:SORT,特殊查询条件key:SQL)</param>
/// <returns></returns>
public static DataSet GetOraclePageList(int currentPage, int pageSize, out int itemCount, string tableName, System.Collections.Generic.Dictionary<string, string> whereList)
{
int rowCount = GetRowCount(SQLDBType.ORACLE, pageSize, out itemCount, tableName, whereList);
if (currentPage <= 1) currentPage = 1;//最小页
if (currentPage >= rowCount) currentPage = rowCount;//最大页
StringBuilder strSql = new StringBuilder();
#region 公用分页查询语句
strSql.Append("select * from ( ");
strSql.Append(" select a.*,rownum as rn from ( ");
strSql.AppendFormat("select * from {0} t where 1=1 ", tableName);
if (whereList != null && whereList.Count > 0)
{
foreach (System.Collections.Generic.KeyValuePair<string, string> w in whereList)
{
if (w.Key == "SQL")
{
strSql.Append(w.Value);
}
else if (w.Key == "ORDER" || w.Key == "SORT")
continue;
else
{
strSql.AppendFormat(" and {0}='{1}'", w.Key, w.Value);
}
}
}
if (whereList.ContainsKey("ORDER"))
{
strSql.AppendFormat(" order by {0} ", whereList["ORDER"]);
if (whereList.ContainsKey("SORT"))
strSql.Append(whereList["SORT"]);
}
strSql.Append(")a ");
strSql.Append(")b");
strSql.AppendFormat(" where b.rn between {0} and {1}", (currentPage - 1) * pageSize + 1, (currentPage - 1) * pageSize + pageSize);
#endregion
return DBUtility.DbHelperOra.Query(strSql.ToString());
}
/// <summary>
/// mysql分页方法
/// </summary>
/// <param name="currentPage">当前页</param>
/// <param name="pageSize">每页显示行数</param>
/// <param name="itemCount">返回总行数</param>
/// <param name="tableName">要获取的表</param>
/// <param name="whereList">Dictionary查询条,key,value键值形式(排序字段key: ORDER,升序降序 key:SORT,特殊查询条件key:SQL)</param>
/// <returns></returns>
public static DataSet GetMySqlPageList(int currentPage, int pageSize, out int itemCount, string tableName, System.Collections.Generic.Dictionary<string, string> whereList)
{
int rowCount = GetRowCount(pageSize, out itemCount, tableName, whereList);
if (currentPage <= 1) currentPage = 1;//最小页
if (currentPage >= rowCount && rowCount>0) currentPage = rowCount;//最大页
StringBuilder strSql = new StringBuilder();
#region 公用分页查询语句
strSql.AppendFormat("select * from {0} where 1=1 ", tableName);
if (whereList != null && whereList.Count > 0)
{
foreach (System.Collections.Generic.KeyValuePair<string, string> w in whereList)
{
if (w.Key == "SQL")
{
strSql.Append(w.Value);
}
else if (w.Key == "ORDER" || w.Key == "SORT")
continue;
else
{
strSql.AppendFormat(" and {0}='{1}'", w.Key, w.Value);
}
}
if (whereList.ContainsKey("ORDER"))
{
strSql.AppendFormat(" order by {0} ", whereList["ORDER"]);
if (whereList.ContainsKey("SORT"))
strSql.Append(whereList["SORT"]);
}
}
strSql.AppendFormat(" LIMIT {0},{1}", (currentPage - 1) * pageSize, pageSize);
#endregion
return DbHelperMySQL.Query(strSql.ToString());
}