在用存储过程做分页搜索有点问题,请大家帮忙!分不够再加!
存储过程: if Exists(Select name From sysobjects Where name='csp_Paging'And type='P')
Drop Procedure dbo.csp_Paging
Go
/**
临时表分页SP.
*/
Create PROC dbo.csp_Paging
@PageSize INT = 20, @PageIndex INT = 1, @DoCount BIT = 0,
@keyWord varchar(200),@inputtype varchar(100)
AS
SET NOCOUNT ON
IF @DoCount = 1 BEGIN
SELECT COUNT(newsId) AS CT FROM news where
@inputtype like '%'+@keyWord+'%'
END
ELSE BEGIN
-- @PageLowerBound:第@PageIndex页的其始索引。
-- @PageUpperBound:第@PageIndex页的终止索引。
DECLARE @PageLowerBound INT, @PageUpperBound INT
-- 建立临时表。
CREATE TABLE #Temp(
TempID INT identity(1,1) PRIMARY KEY,
newsId INT
)
SET @PageLowerBound = (@PageIndex - 1) * @PageSize + 1
SET @PageUpperBound = @PageIndex * @PageSize
-- 提高插入效率。
SET ROWCOUNT @PageUpperBound
INSERT INTO #Temp(newsId)
SELECT newsId FROM news
where @inputtype like '%' + @keyword +'%'
SELECT a.* FROM news a, #Temp t
where @inputtype like '%' + @keyword +'%'and
(a.newsId = t.newsId)
AND (t.TempID between
@PageLowerBound and @PageUpperBound)
-- 显示删除临时表
DROP TABLE #Temp
END
SET NOCOUNT OFF
GO
code:
private void page(bool IsButton)
{
string strPageIndex=string.Empty;
//如果是按钮导航
if(IsButton)
{
strPageIndex=tbPage.Text.ToString();
}
else
{
strPageIndex=Request.QueryString["page"];
}
//如果没有页码则显示第一页
if(strPageIndex=="" || strPageIndex==null || int.Parse(strPageIndex)<=0)
{
strPageIndex="1";
}
//将 页码转换成整形
CurrentPage=int.Parse(strPageIndex);
//指定页面大小
int pageSize=2;
Proc conn=new Proc();
//得到记录总数
TotalCount=conn.GetTotalCounts(this.TextBox1.Text,this.DropDownList1.SelectedValue);
Response.Write(TotalCount);
TotalPage=Convert.ToInt32(Math.Ceiling(TotalCount/pageSize));
//判断页码的有效性
if(CurrentPage>TotalPage)
{
CurrentPage=TotalPage;
}
HyperLink2.NavigateUrl="adminUser.aspx?page="+(CurrentPage-1)+"";
HyperLink3.NavigateUrl="adminUser.aspx?page="+(CurrentPage+1)+"";
HyperLink4.NavigateUrl="adminUser.aspx?page="+TotalPage+"";
DataTable reader=conn.spPages(pageSize,CurrentPage,this.TextBox1.Text,this.DropDownList1.SelectedValue);
dgUser.DataSource=reader;
dgUser.DataBind();
} --------------------编程问答-------------------- 没有人知道??大家帮帮忙哦! --------------------编程问答-------------------- 你在网上找个可以用的代码不就得了!!
帮你顶!! --------------------编程问答-------------------- 在网上找???你帮我找个... --------------------编程问答-------------------- 给个分页的存储过程给你,不过没有建临时表的过程。可以参考下分页的代码:
CREATE PROCEDURE Search
(
@TableName nvarchar(1000),
@SearchContent nvarchar(1000),
@WhereString nvarchar(1000),
@OrderString nvarchar(1000),
@PageSize int,
@PageIndex int,
@PageCount int output,
@RecordCount int output
)
AS
declare @SQL nvarchar(1000)
declare @str nvarchar(1000)
set @str=N'select @RecordCount=count(*) from '+@TableName+' '+@WhereString
exec sp_executesql @str,
N'@RecordCount int output',
@RecordCount output
set @PageCount=ceiling(@RecordCount*1.0/@PageSize)
if @PageIndex = 0 or @PageCount<=1
begin
set @SQL='select top '+str(@PageSize)+' '+@SearchContent+' from '+@TableName+' '+@WhereString+' '+@OrderString+' desc '
end
else
begin
if @PageIndex = @PageCount -1
begin
set @SQL='select '+@SearchContent+' from ( select top '+str(@RecordCount - @PageSize * @PageIndex)+' * from '+@TableName+' '+@WhereString+' '+@OrderString+' asc ) TempTable '+@OrderString+' desc '
end
else
begin
set @SQL='select top '+str(@PageSize) +' '+@SearchContent+' from ( select top '+str(@RecordCount - @PageSize * @PageIndex)+' * from '+@TableName+' '+@WhereString+' '+@OrderString+' asc ) TempTable '+@OrderString+' desc '
end
end
exec(@SQL)
GO
--------------------编程问答-------------------- 网上多的很,我看你的分页的效率不怎么高,也存在一个问题
为何要创建一个临时表?根本没必要。 --------------------编程问答-------------------- 给你个带注释的,这个写的很不错,楼主可以参考一下
--------------------编程问答-------------------- Magic_YJL 可以再给个调用存储过程的代码吗?马上就要交项目了... --------------------编程问答--------------------
CREATE proc getdataset
@TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
@TableName Varchar(30), --搜索的表名
@SelectWhere Varchar(500)='',--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
@SelectOrderId Varchar(20),--表主键字段名。比如:id
@SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
@intPageNo int=1, --页号
@intPageSize int=10 ,--每页显示数
@RecordCount int OUTPUT --总记录数(存储过程输出参数)
as
declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600)
set nocount on--关闭计数
set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere
execute sp_executesql
@TmpSelect, --执行上面的sql语句
N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
@RecordCount OUTPUT
if (@RecordCount = 0) --如果没有贴子,则返回零
return 0
/*判断页数是否正确*/
if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
return (-1)
set nocount off--打开计数
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO
楼主就参照这个吧,挺不错的 --------------------编程问答-------------------- 关注并学习下
呵呵。。。 --------------------编程问答-------------------- 给你个带注释的,这个写的很不错,楼主可以参考一下
补充:.NET技术 , ASP.NET