//执行存储过程方法
    public DataTable ExecProcReturn(string strName, SqlParameter[] sqlParas)
    {
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Myconnection"].ToString());
        conn.Open();
        using (SqlCommand com = new SqlCommand())
        {
            com.Connection = conn;
            com.CommandText = strName;
            com.CommandType = CommandType.StoredProcedure;
            if (sqlParas != null && sqlParas.Length > 0)
            {
                foreach (SqlParameter sqlPara in sqlParas)
                {
                    com.Parameters.Add(sqlPara);
                }
            }
            SqlDataAdapter da = new SqlDataAdapter(com);
            da.Fill(dt);//调试此处提示TOP N 值不得为负
        }
        conn.Close();
        return dt;
    }sql如下:
create proc xp_Page
@PageIndex int
as
declare @PageSize int
set @PageSize=5
if(@PageIndex=1)
begin
select top (@PageSize) [DiaryTitle],[DiaryTime],[DiaryAuthor] from [Diary] order by [DiaryID] desc
end
else
begin
select top (@PageSize) [DiaryTitle],[DiaryTime],[DiaryAuthor] from [Diary] where [DiaryId] not in
(select top ((@PageIndex-1)*(@PageSize)) [DiaryId] from [Diary] order by [DiaryID] desc)
order by [DiaryId] desc
end这是什么原因啊,求解,谢谢!

解决方案 »

  1.   

    ((@PageIndex-1)*(@PageSize)) 
    这里为付数了
    如果传过来0的话
      

  2.   

    其他地方不会出现负数 好好检查下传过去的 @PageIndex 
      

  3.   

    create procedure xp_Page 
    (@startIndex int,
    @endIndex int)
    asbegin
     with temptbl as (
    SELECT ROW_NUMBER() OVER (ORDER BY DiaryId desc)AS Row, O.[DiaryTitle],O.[DiaryTime],O.[DiaryAuthor] from Diary )
     SELECT * FROM temptbl where Row between @startIndex and @endIndex
    end
      

  4.   

    DECLARE @pagenum AS INT, @pagesize AS INT 
    SET @pagenum = 2 
    SET @pagesize = 3 
    SELECT * 
    FROM (SELECT ROW_NUMBER() OVER(ORDER BY newsid DESC) AS rownum, 
            newsid, topic, hits 
          FROM news) AS D 
    WHERE rownum BETWEEN (@pagenum-1)*@pagesize+1 AND @pagenum*@pagesize 
    ORDER BY newsid DESC
      

  5.   

    同意3楼 多加个判断最后直接return
      

  6.   

    页面获取的时候,你pagesize定义个初值
      

  7.   

    页面获取的时候,你pageindex定义个初值