- //实例化Connection对象
- SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
- //实例化Command对象
- SqlCommand command = new SqlCommand("select * from UserInfo where sex=@sex and age>@age", connection);
- //第一种添加查询参数的例子
- command.Parameters.AddWithValue("@sex",true);
- //第二种添加查询参数的例子
- SqlParameter parameter = new SqlParameter("@age", SqlDbType.Int);//注意UserInfo表里age字段是int类型的
- parameter.Value = 30;
- command.Parameters.Add(parameter);//添加参数
- //实例化DataAdapter
- SqlDataAdapter adapter = new SqlDataAdapter(command);
- DataTable data = new DataTable();
如果每页显示5条记录,那么第n页显示的数据记录的公式应该是:
select top 5 * from UserInfo where UserId not in
(select top (n-1)*5 UserID from UserInfo order by UserID asc)
order by UserID asc
假如总共有m条记录,每页显示n条记录(这里m,n都是大于0的整数)那么需要显示全部记录所用到的页数page为:
- page=(m%n)==0?(m/n):(m/n+1);
本文详细介绍了如何使用SQL查询并结合分页公式获取特定页面的数据记录,包括参数化查询和分页逻辑的实现。
1330

被折叠的 条评论
为什么被折叠?



