参数化SQL语句 分页查询SQL语句

  1. //实例化Connection对象
  2. SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
  3. //实例化Command对象
  4. SqlCommand command = new SqlCommand("select * from UserInfo where sex=@sex and age>@age", connection);
  5. //第一种添加查询参数的例子
  6. command.Parameters.AddWithValue("@sex",true);
  7. //第二种添加查询参数的例子
  8. SqlParameter parameter = new SqlParameter("@age", SqlDbType.Int);//注意UserInfo表里age字段是int类型的
  9. parameter.Value = 30;
  10. command.Parameters.Add(parameter);//添加参数
  11. //实例化DataAdapter
  12. SqlDataAdapter adapter = new SqlDataAdapter(command);
  13. 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为:

  1. page=(m%n)==0?(m/n):(m/n+1);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值