(
@tblName nvarchar(200), ----要顯示的表或多個表的連接
@fldName nvarchar(500) = '*', ----要顯示的字段列表
@pageSize int = 1, ----每頁顯示的記錄個數
@page int = 10, ----要顯示那一頁的記錄
@pageCount int = 1 output, ----查詢結果分頁後的總頁數
@Counts int = 1 output, ----查詢到的記錄數
@fldSort nvarchar(200) = null, ----排序字段列表或條件
@Sort bit = 0, ----排序方法,0爲升序,1爲降序(如果是多字段排列Sort指代最後一個排序字段的排列順序(最後一個排序字段不加排序標記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar(1000) = null, ----查詢條件,不需where
@ID nvarchar(150), ----主表的主鍵
@Dist bit = 0 , ----是否添加查詢字段的 DISTINCT 默認0不添加/1添加
@strSql nvarchar(1000) = '' output
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放動態生成的SQL語句
Declare @strTmp nvarchar(1000) ----存放取得查詢結果總數的查詢語句
Declare @strID nvarchar(1000) ----存放取得查詢開頭或結尾ID的查詢語句
Declare @strSortType nvarchar(10) ----數據排序規則A
Declare @strFSortType nvarchar(10) ----數據排序規則B
Declare @SqlSelect nvarchar(50) ----對含有DISTINCT的查詢進行SQL構造
Declare @SqlCounts nvarchar(50) ----對含有DISTINCT的總數查詢進行SQL構造
if @Dist = 0
begin
set @SqlSelect = 'select '
set @SqlCounts = 'Count(*)'
end
else
begin
set @SqlSelect = 'select distinct '
set @SqlCounts = 'Count(DISTINCT '+@ID+')'
end
if @Sort=0
begin
set @strFSortType=' ASC '
set @strSortType=' DESC '
end
else
begin
set @strFSortType=' DESC '
set @strSortType=' ASC '
end
--------生成查詢語句--------
--此處@strTmp爲取得查詢結果數量的語句
if @strCondition is null or @strCondition='' --沒有設置顯示條件
begin
set @sqlTmp = @fldName + ' From ' + @tblName
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
set @strID = ' From ' + @tblName
end
else
begin
set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end
----取得查詢結果總數量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
declare @tmpCounts int
if @Counts = 0
set @tmpCounts = 1
else
set @tmpCounts = @Counts
--取得分頁總數
set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
/**//**當前頁大於總頁數 取最後一頁**/
if @page>@pageCount
set @page=@pageCount
--/*-----數據分頁2分處理-------*/
declare @pageIndex int --總數/頁大小
declare @lastcount int --總數%頁大小
set @pageIndex = @tmpCounts/@pageSize
set @lastcount = @tmpCounts%@pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1
else
set @lastcount = @pagesize
--//***顯示分頁
if @strCondition is null or @strCondition='' --沒有設置顯示條件
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strFSortType+')'
+' order by '+ @fldSort +' '+ @strFSortType
end
else
begin
set @page = @pageIndex-@page+1 --後半部分數據處理
if @page <= 1 --最後一頁數據顯示
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+')'
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end
else --有查詢條件
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from '+@tblName
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' Where (1>0) ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType+')'
+' ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
end
else
begin
set @page = @pageIndex-@page+1 --後半部分數據處理
if @page <= 1 --最後一頁數據顯示
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+')'
+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end
------返回查詢結果-----
set @strSql = @strTmp
exec sp_executesql @strTmp
--print @strTmp
using System;
using System.Data.SqlClient;
using System.Data;
namespace Test
{
/// <summary>
/// 執行短連接存儲過程
/// </summary>
public class Procedure
{
SqlConnection conn;
public Procedure()
{
conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["bizConStr"]);
}
/// <summary>
/// 通用分頁數據讀取函數
/// </summary>
/// <param name="_tblName">查詢的表/表聯合</param>
/// <param name="_fldName">要查詢的字段名</param>
/// <param name="_fldSort">排序字段</param>
/// <param name="_Sort">排序順序0降序1升序</param>
/// <param name="_pageSize">每頁數據大小</param>
/// <param name="_page">當前第幾頁</param>
/// <param name="_strCondition">過濾條件</param>
/// <param name="_ID">主表主鍵</param>
/// <param name="_pageCount">總頁數</param>
/// <param name="_RsCounts">記錄總數</param>
public DataSet ExecProCutPageRetrunDS
(string _tblName,
string _fldName,
string _fldSort,
int _Sort,
int _pageSize,
int _page,
string _strCondition,
string _ID,
int _Dist,
out int _pageCount,
out int _RsCounts,
out string strSql)
{
DataSet ds = new DataSet();
SqlCommand cmd=new SqlCommand("proc_ListPage",conn);
cmd.CommandType = CommandType.StoredProcedure;
_pageCount = -1;
_RsCounts = -1;
strSql = null;
cmd.Parameters.Add("@tblName", SqlDbType.NVarChar, 200);
cmd.Parameters["@tblName"].Value = _tblName;
cmd.Parameters.Add("@fldName", SqlDbType.NVarChar, 500);
cmd.Parameters["@fldName"].Value = _fldName;
cmd.Parameters.Add("@pageSize", SqlDbType.Int);
cmd.Parameters["@pageSize"].Value = _pageSize;
cmd.Parameters.Add("@page", SqlDbType.Int);
cmd.Parameters["@page"].Value = _page;
cmd.Parameters.Add("@fldSort", SqlDbType.NVarChar, 200);
cmd.Parameters["@fldSort"].Value = _fldSort;
cmd.Parameters.Add("@Sort", SqlDbType.Bit);
cmd.Parameters["@Sort"].Value = _Sort;
cmd.Parameters.Add("@strCondition", SqlDbType.NVarChar, 1000);
cmd.Parameters["@strCondition"].Value = _strCondition;
cmd.Parameters.Add("@ID", SqlDbType.NVarChar, 150);
cmd.Parameters["@ID"].Value = _ID;
cmd.Parameters.Add("@Dist", SqlDbType.Bit);
cmd.Parameters["@Dist"].Value = _Dist;
cmd.Parameters.Add("@Counts", SqlDbType.Int, 0);
cmd.Parameters["@Counts"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@pageCount", SqlDbType.Int, 0);
cmd.Parameters["@pageCount"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@strSql", SqlDbType.NVarChar, 1000);
cmd.Parameters["@strSql"].Direction = ParameterDirection.Output;
try
{
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
adapter.Fill(ds);
_pageCount = (int)cmd.Parameters["@pageCount"].Value;
_RsCounts = (int)cmd.Parameters["@Counts"].Value;
strSql = (string)cmd.Parameters["@strSql"].Value;
//System.Web.HttpContext.Current.Response.Write("<br>"+strSql+"<br>");
}
catch(Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.ToString() );
ds = null;
}
return ds;
}
}
}