创建存储过程
- Create PROCEDURE [dbo].[PROCE_SQL2005PAGECHANGE]
- (
- @TableName varchar(50), --表名
- @ReFieldsStr varchar(200) = '*', --字段名(全部字段为*)
- @OrderString varchar(200), --排序字段(必须!支持多字段不用加order by)
- @WhereString varchar(500) =N'', --条件语句(不用加where)
- @PageSize int, --每页多少条记录
- @PageIndex int = 1 , --指定当前为第几页
- @TotalRecord int output --返回总记录数
- )
- AS
- BEGIN
- --处理开始点和结束点
- Declare @StartRecord int;
- Declare @EndRecord int;
- Declare @TotalCountSql nvarchar(500);
- Declare @SqlString nvarchar(2000);
- set @StartRecord = (@PageIndex-1)*@PageSize + 1
- set @EndRecord = @StartRecord + @PageSize - 1
- SET @TotalCountSql= N'select @TotalRecord = count(*) from ' + @TableName;--总记录数语句
- SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句
- --
- IF (@WhereString! = '' or @WhereString!=null)
- BEGIN
- SET @TotalCountSql=@TotalCountSql + ' where '+ @WhereString;
- SET @SqlString =@SqlString+ ' where '+ @WhereString;
- END
- --第一次执行得到
- --IF(@TotalRecord is null)
- -- BEGIN
- EXEC sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output;--返回总记录数
- -- END
- ----执行主语句
- set @SqlString ='select * from ' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' + ltrim(str(@EndRecord));
- Exec(@SqlString)
- END
C#中调用方法
- private static readonly string ConnString = "";//你懂的
- /// <summary>
- /// 存储过程分页查询
- /// </summary>
- /// <param name="TableName">表名</param>
- /// <param name="ReFieldsStr">字段名(全部字段为*)</param>
- /// <param name="OrderString">排序字段(必须!支持多字段不用加order by)</param>
- /// <param name="WhereString">条件语句(不用加where)</param>
- /// <param name="index">每页多少条记录</param>
- /// <param name="pagesize">指定当前为第几页</param>
- /// <param name="pg">返回总记录数</param>
- /// <returns></returns>
- public static DataSet Select_Certificate(string TableName, string ReFieldsStr, string OrderString, string WhereString, int index, int pagesize, ref int pg)
- {
- int i = 0;
- SqlConnection sqlCon = new SqlConnection(ConnString);
- SqlDataAdapter da = new SqlDataAdapter("PAGECHANGE", sqlCon);
- SqlParameter para0 = new SqlParameter("@TableName", TableName);
- SqlParameter para1 = new SqlParameter("@ReFieldsStr", ReFieldsStr);
- SqlParameter para2 = new SqlParameter("@OrderString", OrderString);
- SqlParameter para3 = new SqlParameter("@WhereString", WhereString);
- SqlParameter para4 = new SqlParameter("@PageSize", pagesize);
- SqlParameter para5 = new SqlParameter("@PageIndex", index);
- SqlParameter outputpara = new SqlParameter("@TotalRecord", SqlDbType.Int);
- outputpara.Direction = ParameterDirection.Output;
- da.SelectCommand.Parameters.Add(para0);
- da.SelectCommand.Parameters.Add(para1);
- da.SelectCommand.Parameters.Add(para2);
- da.SelectCommand.Parameters.Add(para3);
- da.SelectCommand.Parameters.Add(para4);
- da.SelectCommand.Parameters.Add(para5);
- da.SelectCommand.Parameters.Add(outputpara);
- da.SelectCommand.CommandType = CommandType.StoredProcedure;
- DataSet ds = new DataSet();
- try
- {
- sqlCon.Open();
- da.Fill(ds);
- i = Convert.ToInt32(outputpara.Value);
- if (i % pagesize > 0)
- {
- pg = i / pagesize + 1;
- }
- else
- {
- pg = i / pagesize;
- }
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message, ex);
- }
- finally
- {
- sqlCon.Close();
- sqlCon.Dispose();
- }
- return ds;
- }