日期:2014-05-18  浏览次数:20420 次

数据分页,一个很奇怪的现象
部分代码:
C# code
       
                string strpagenum = context.Request["pagenum"];
                int ipagenum = Convert.ToInt32(strpagenum);
                using (SqlConnection conn = new SqlConnection(@"Data Source=123-PC\SQL2005;Initial Catalog=ZJJ;User ID=sa"))
                {
                    conn.Open();
                    using(SqlCommand cmd=conn.CreateCommand())
                    {
                        cmd.CommandText = "select * from(select id,title,price,imgurl,Row_Number() over(order by id) as rownum from  tb_products)a where a.rownum>=" + ((ipagenum - 1) * 15) + 1 + "and rownum<=" + ipagenum * 15 + " ";
                        DataSet ds = new DataSet();
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        adapter.Fill(ds);
                        DataTable table = ds.Tables[0];

                        int count = table.Rows.Count;



注:tb_products表有30行数据

断点调试后发现
strpagenum 为 1 的时候 正常 count=15 
strpagenum 为 2 count 查不出任何数据 count=0

用SQL SERVER 测试

SQL code

select * from(select id,title,price,imgurl,Row_Number() over(order by id) as rownum from  tb_products)a where a.rownum>= ((1 - 1) * 15) + 1 and rownum<=1 * 15

select * from(select id,title,price,imgurl,Row_Number() over(order by id) as rownum from  tb_products)a where a.rownum>= ((2 - 1) * 15) + 1 and rownum<=2 * 15



数据正常

第一页正常,数据都加载出来了,第二页就不OK了,这很奇怪。


------解决方案--------------------
探讨

我试了一下你的sql语句
int ipagenum = 1;
select * from(select id,title,price,imgurl,Row_Number() over(order by id) as rn from tb_products)a where a.rn>=01and a.rn<=15

int ipagenum = 2;
select * from……

------解决方案--------------------
将你原来的字符串改一下,你之前那个没有把ipagenum 的计算结果转为字符串,成了字符串拼接了
改成如下
C# code

"select * from(select id,title,price,imgurl,Row_Number() over(order by id) as rownum from  tb_products)a where a.rownum>=" + (((ipagenum - 1) * 15) + 1).ToString() + " and rownum<=" + (ipagenum * 15).ToString() + " ";