日期:2014-05-17  浏览次数:20488 次

调用分页的存储过程 返回0行数据

create procedure DivPage
@strSql varchar(800),
@nPageSize int,
@nPageCount int
as
    SET NOCOUNT ON
    DECLARE @P1 INT,
    @nRowCount INT

  --//注意 :@scrollopt = 1 会取得Select 的时候的总行数 
    EXEC sp_cursoropen @P1 OUTPUT, @strSql, @scrollopt = 2, @ccopt = 335873, @rowcount = @nRowCount OUTPUT

    IF (@P1 != 0)
    BEGIN
--Select @nRowCount AS nRecordCount, ceiling(1.0 * @nRowCount / @nPageSize) AS nPageCount, @nPageCount AS nPage
SET @nPageCount = (@nPageCount - 1) * @nPageSize + 1
EXEC sp_cursorfetch @P1, 32, @nPageCount, @nPageSize  
EXEC sp_cursorclose @P1
    END
GO



 using(comm.Connection =new SqlConnection(DBCommon.constr) )
    {
    string query = "select * from person";
       comm.Connection.Open();
       comm.CommandText = "DivPage";
       comm.CommandType = CommandType.StoredProcedure;
   comm.Parameters.Add(new SqlParameter("@strsql", query));
   comm.Parameters.Add(new SqlParameter("@nPageSize",10));
   comm.Parameters.Add(new SqlParameter("@nPageCount",1));
   SqlDataAdapter da = new SqlDataAdapter();
       da.SelectCommand = comm;
       da.Fill(dt); //结果dt为空没有结果 在数据库直接执行 exec DivPage 'select * from person',10,1 能出结果



------解决方案--------------------
/// <summary>
    /// 获取DataSet对象
    /// </summary>
    /// <param name="queryString">T-SQL语句</param>