asp.net Repeater之非常好的数据分页

2019-05-11 21:49:00于海丽

/// <param name="strSQL">传入的Sql语句</param>
/// <returns>DataTable</returns>
public static DataTable ReturnDataTable(string strSQL)
{
SqlConnection conn = OpenDataBase();

DataTable table = new DataTable();

SqlDataAdapter da = null;

try
{
da = new SqlDataAdapter(strSQL, conn);

da.Fill(table);

da.Dispose();

CloseDataBase(conn);

return table;
}
catch (Exception e)
{
da.Dispose();

table.Dispose();

CloseDataBase(conn);

throw new Exception(e.Message);
}
finally
{
da.Dispose();

CloseDataBase(conn);
}
}

/// <summary>
/// 返回DataTable记录集,并且实现分页功能
/// </summary>
/// <param name="sqls">传入SQL语句</param>
/// <param name="currentpage">当前页</param>
/// <param name="pagesize">每页分页大小</param>
/// <param name="table">填充数据库表名称</param>
/// <returns></returns>
public static DataTable ReturnDataTablePage(string sqls, int currentpage, int pagesize, string table)
{
SqlConnection conn = OpenDataBase();

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter(sqls, conn);

try
{
int startcount;

if (currentpage < 1)
{
startcount = currentpage * pagesize;
}
else
{
startcount = (currentpage - 1) * pagesize;
}

da.Fill(ds, startcount, pagesize, table);

da.Dispose();

CloseDataBase(conn);

return ds.Tables[0];
}
catch (Exception ex)
{
da.Dispose();

CloseDataBase(conn);

ds.Dispose();

throw new Exception(ex.Message);
}
finally
{
da.Dispose();

CloseDataBase(conn);
}
}

#endregion

#region 数据库公用方法集

/// <summary>
/// 返回记录总数 传入带有count(主键)统计的Sql语句
/// </summary>
/// <param name="StrSql">传入带有count(主键)统计的Sql语句</param>
/// <returns></returns>
public static string RecordCounts(string StrSql)
{
string ProcInfo = "0";

SqlConnection conn = OpenDataBase();

SqlCommand comm = new SqlCommand(StrSql, conn);

comm.CommandTimeout = 120;

SqlDataReader DataReaders = comm.ExecuteReader(); //返回值

if (DataReaders.Read())
{
ProcInfo = DataReaders[0].ToString();
}

DataReaders.Close();

comm.Dispose();

CloseDataBase(conn);

return ProcInfo;
}

#endregion
}
}

using System;
using System.Collections.Generic;
using System.Text;

namespace Sql_Function
{
public class PublicFunction
{
/// <summary>
/// 判断是否为数字 TRUE代表不是数字,False代表是数字
/// </summary>
/// <param name="s">字符串</param>