IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME=‘PageTest‘)
DROP PROC PageTest
GO
CREATE PROCEDURE [dbo].[PageTest]
@Table VARCHAR(1000), --表名,多表是请使用 tA a inner join tB b On a.AID = b.AID
@TIndex NVARCHAR(100), --主键,可以带表头 a.AID
@Column NVARCHAR(2000) = ‘*‘,--读取字段
@Sql NVARCHAR(3000) = ‘‘,--Where条件
@PageIndex INT = 1, --开始页码
@PageSize INT = 10, --页大小
@Sort NVARCHAR(200) = ‘‘ --排序字段
AS
--_PageTest ‘HoursMIPK‘,‘id‘,‘*‘,‘‘,1,10,‘id‘
DECLARE @strWhere VARCHAR(2000)
DECLARE @strsql NVARCHAR(3900)
IF @Sql IS NOT NULL AND len(LTRIM(RTRIM(@Sql)))>0
BEGIN
SET @strWhere = ‘ WHERE ‘ + @Sql + ‘ ‘
END
ELSE
BEGIN
SET @strWhere = ‘‘
END
IF (charindex(LTRIM(RTRIM(@TIndex)),@Sort)=0)
BEGIN
IF(@Sort=‘‘)
SET @Sort = @TIndex + ‘ DESC ‘
ELSE
SET @Sort = @Sort+ ‘ , ‘
+@TIndex + ‘ DESC ‘
END
IF @PageIndex < 1
SET @PageIndex = 1
IF @PageIndex = 1 --第一页提高性能
BEGIN
SET @strsql = ‘SELECT TOP ‘ + str(@PageSize) +‘ ‘
+@Column+ ‘ FROM ‘ + @Table + ‘ ‘ + @strWhere + ‘ ORDER BY ‘+ @Sort
END
ELSE
BEGIN
DECLARE @START_ID NVARCHAR(50)
DECLARE @END_ID NVARCHAR(50)
SET @START_ID = convert(NVARCHAR(50),(@PageIndex - 1) * @PageSize + 1)
SET @END_ID = convert(NVARCHAR(50),@PageIndex * @PageSize)
FROM (SELECT ROW_NUMBER() OVER(ORDER BY ‘
+@Sort+‘) AS RowNum,
FROM ‘
+@Table +‘ WITH(NOLOCK) ‘ + @strWhere +‘) AS D
END
EXEC(@strsql)
PRINT @strsql
SET @strsql = ‘SELECT Count(1) as TotalRecords FROM ‘ + @Table +‘ WITH(NOLOCK) ‘ + @strWhere
PRINT @strsql
EXEC(@strsql)
private void Form2_Load(object sender, EventArgs e)
{
BindDataWithPage(1);
}
//总记录数
public int RecordCount = 0;
private string strConn = @"Data Source=USER-20151123RD;Initial Catalog=MVC;Integrated Security=True";
//"Server=localhost;database=Db_TonyPaging;uid=sa;pwd=sa;";
private string strProcedure = "PageTest";
/// <summary>
/// 绑定第Index页的数据
/// </summary>
/// <param name="Index"></param>
private void BindDataWithPage(int Index)
{
allenPage1.PageIndex = Index;
//winFormPager1.PageSize = 10;; ;
DataTable dt = GetData(strConn, strProcedure, Index, allenPage1.PageSize);
dataGridView1.DataSource = dt;
//获取并设置总记录数
allenPage1.RecordCount = RecordCount;
}
/// <summary>
/// 获取数据源
/// </summary>
/// <param name="conn"></param>
/// <param name="strProcedure"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
private DataTable GetData(string conn, string strProcedure, int pageIndex, int pageSize)
{
using (SqlConnection connection = new SqlConnection(conn))
{
SqlCommand command = new SqlCommand(strProcedure, connection);
command.CommandType = CommandType.StoredProcedure;//采用存储过程
//存储过程参数
command.Parameters.Add("@Table", SqlDbType.NVarChar, 1000).Value = "Users";
command.Parameters.Add("@TIndex", SqlDbType.NVarChar, 100).Value = "ID";
command.Parameters.Add("@Column", SqlDbType.NVarChar, 2000).Value = "*";
command.Parameters.Add("@Sql", SqlDbType.NVarChar, 3000).Value = " 1=1 ";
command.Parameters.Add("@PageIndex", SqlDbType.Int, 8).Value = pageIndex.ToString();
command.Parameters.Add("@PageSize", SqlDbType.Int, 8).Value = pageSize.ToString();
command.Parameters.Add("@Sort", SqlDbType.NVarChar, 200).Value = " ID desc";
//打开连接
if (connection.State != ConnectionState.Open) { connection.Open(); }
try
{
//填充数据
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
//获取总记录数
RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
//返回数据集
return ds.Tables[0];
}
catch (SqlException err)
{
MessageBox.Show(err.Message);
return null; ;
}
finally
{
connection.Close();
}
}
}
private void allenPage1_PageIndexChanged(object sender, EventArgs e)
{
BindDataWithPage(allenPage1.PageIndex);
}