这是个典型的top分页查询
@currentpageindex 就是gridview的current page indexUSE northwindALTER PROCEDURE [dbo].[GetProductsByPagenum]

(
@pagesize int = 5,
@currentpageindex int=1,
@rowcount int output
)

AS
/* SET NOCOUNT ON */

select @rowcount=count(*) from Products select top (@pagesize) * 
from Products
where ProductID not in (select top (@pagesize*@currentpageindex) ProductID from Products)
return @@rowcount
当我在sql中执行(因为gridview pageindex是从0开始计数的)USE [Northwind]
GODECLARE @return_value int,
@rowcount intEXEC @return_value = [dbo].[GetProductsByPagenum]
@pagesize = 10,
@currentpageindex = 0,
@rowcount = @rowcount OUTPUTSELECT @rowcount as N'@rowcount'SELECT 'Return Value' = @return_valueGO查询出来的结果是10条,  ProductID是从1到10
但是当我用sqlcommand调用这个存储过程的时候 查出来的行数为5 ProductID是从1到5 DataSet ds = new DataSet();
            string connectionString = @"Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True";
            SqlConnection cn = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("GetProductsByPagenum", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@pagesize", SqlDbType.Int, this.GridView1.PageSize);
            param[1] = new SqlParameter("@currentpageindex", SqlDbType.Int, this.GridView1.PageIndex);
            param[2] = new SqlParameter("@rowcount", SqlDbType.Int);
            param[2].Direction = ParameterDirection.Output;
            cmd.Parameters.AddRange(param);            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            adapt.Fill(ds);
            count = Convert.ToInt32(cmd.Parameters[2].Value);
            return ds.Tables[0];我找了下,没发现原因在哪......