1.封装实体类

2.写查询方法

  1. //SubjectData类
  2. public List<Subject> Select(string name)
  3. {
  4. List<Subject> list = new List<Subject>();
  5. cmd.CommandText = "select *from Subject where SubjectName like @a ";
  6. cmd.Parameters.Clear();
  7. cmd.Parameters.Add("@a","%"+name+"%");
  8. conn.Open();
  9. SqlDataReader dr = cmd.ExecuteReader();
  10. if (dr.HasRows)
  11. {
  12. while (dr.Read())
  13. {
  14. Subject s = new Subject();
  15. s.SubjectCode = dr[].ToString();
  16. s.SubjectName = dr[].ToString();
  17. list.Add(s);
  18. }
  19. }
  20. conn.Close();
  21.  
  22. return list;
  23. }
  24. //StudentData类
  25. /// <summary>
  26. /// 查询方法
  27. /// </summary>
  28. /// <param name="tsql">SQL语句</param>
  29. /// <param name="hh">哈希表</param>
  30. /// <returns></returns>
  31. public List<Student> Select(string tsql,Hashtable hh)
  32. {
  33. List<Student> list = new List<Student>();
  34. cmd.CommandText = tsql;
  35. cmd.Parameters.Clear();
  36. foreach( string s in hh.Keys)
  37. {
  38. cmd.Parameters.Add(s,hh[s]);
  39. }
  40. conn.Open();
  41. SqlDataReader dr = cmd.ExecuteReader();
  42. if (dr.HasRows)
  43. {
  44. while (dr.Read())
  45. {
  46. Student s = new Student();
  47. s.Code = dr[].ToString();
  48. s.Name = dr[].ToString();
  49. s.Sex = Convert.ToBoolean(dr[]);
  50. s.Birthday = Convert.ToDateTime(dr[]);
  51. s.SubjectCode = dr[].ToString();
  52. s.Nation = dr[].ToString();
  53. list.Add(s);
  54. }
  55. }
  56. conn.Close();
  57. return list;
  58. }
  59.  
  60. 查询方法

3.Page_Load部分,最大页方法

  1. int PageCount = ; //每页显示条数
  2. Hashtable hs = new Hashtable();
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. if(!IsPostBack)
  6. {
  7. string tsql = "select top "+PageCount+" *from Student";//查询前PageCount条数据
  8. //Repeater1数据源指向
  9. List<Student> list = new StudentData().Select(tsql,hs);
  10. Repeater1.DataSource = list;
  11. Repeater1.DataBind();
  12. Label2.Text = "";//第一页
  13. //获取最大页
  14. string sql = "select *from Student";
  15. Label3.Text = MaxPageNumber(sql,hs).ToString();
  16. for (int i = ; i <= MaxPageNumber(sql,hs); i++)//给可快速跳转列表框赋值
  17. {
  18. DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
  19. }
  20. }
  21. }
  22.  
  23. Page_Load
  1. public int MaxPageNumber(string sql, Hashtable hs)
  2. {
  3. List<Student> list = new StudentData().Select(sql, hs);//查询所有数据
  4.  
  5. double de = list.Count / (PageCount * 1.0);
  6.  
  7. int aa = Convert.ToInt32(Math.Ceiling(de));//取上限
  8. return aa;
  9. }
  10.  
  11. 获取最大页

4.根据组合查询拼接语句方法

  1. /// <summary>
  2. ///
  3. /// </summary>
  4. /// <param name="sql">拼接查询前PageCount条数据的语句</param>
  5. /// <param name="sql2">查询所有的语句</param>
  6. /// <param name="tj">用于分页查询与sql等拼接</param>
  7. /// <param name="count">判断前几项是否为空</param>
  8. private void Tsql(out string sql, out string sql2,out string tj,out int count)
  9. {
  10. count = ;
  11. sql = "select top " + PageCount + " *from Student";
  12. sql2 = "select *from Student";
  13. tj = "";
  14. //性别不为空
  15. if (!string.IsNullOrEmpty(tb_sex.Text.Trim()))
  16. {//判断输入的是男是女,其它输入默认为未输入内容
  17. if (tb_sex.Text.Trim() == "男")
  18. {
  19. sql += " where Sex = @a";
  20. sql2 += " where Sex = @a";
  21. tj += " where Sex = @a";
  22. hs.Add("@a", "true");
  23. count++;
  24. }
  25. else if (tb_sex.Text.Trim() == "女")
  26. {
  27. sql += " where Sex = @a";
  28. sql2 += " where Sex = @a";
  29. tj += " where Sex = @a";
  30. hs.Add("@a", "false");
  31. count++;
  32. }
  33. }
  34. //年龄不为空
  35. if (!string.IsNullOrEmpty(tb_age.Text.Trim()))
  36. {
  37. int a = DateTime.Now.Year;//获取当前时间的年
  38. try//确保输入的是数字
  39. {
  40. int ag = Convert.ToInt32(tb_age.Text.Trim());
  41. int g = a - ag;
  42. DateTime d = Convert.ToDateTime(g.ToString() + "-1-1");
  43. if (DropDownList3.SelectedValue == ">=")//小于或等于您输入的年龄,即大于或等于某个时间
  44. {
  45. if (count == )//前面的一项未输入(性别)
  46. {
  47. sql += " where Birthday " + DropDownList3.SelectedValue + "@b";
  48. sql2 += " where Birthday " + DropDownList3.SelectedValue + "@b";
  49. tj += " where Birthday " + DropDownList3.SelectedValue + "@b";
  50. }
  51. else
  52. {
  53. sql += " and Birthday " + DropDownList3.SelectedValue + "@b";
  54. sql2 += " and Birthday " + DropDownList3.SelectedValue + "@b";
  55. tj += " and Birthday " + DropDownList3.SelectedValue + "@b";
  56. }
  57. hs.Add("@b", d);
  58. }
  59. else//大于或等于您输入的年龄,即小于或等于某个时间
  60. {
  61. DateTime dd = Convert.ToDateTime(g.ToString() + "-12-31");
  62. if (count == )
  63. {
  64. sql += " where Birthday " + DropDownList3.SelectedValue + "@b";
  65. sql2 += " where Birthday " + DropDownList3.SelectedValue + "@b";
  66. tj += " where Birthday " + DropDownList3.SelectedValue + "@b";
  67. }
  68. else
  69. {
  70. sql += " and Birthday " + DropDownList3.SelectedValue + "@b";
  71. sql2 += " and Birthday " + DropDownList3.SelectedValue + "@b";
  72. tj += " and Birthday " + DropDownList3.SelectedValue + "@b";
  73. }
  74. hs.Add("@b", dd);
  75. }
  76. count++;
  77. }
  78. catch
  79. {
  80. }
  81. }
  82. if (!string.IsNullOrEmpty(tb_s.Text.Trim()))//判断专业是否为空
  83. {
  84. List<Subject> li = new SubjectData().Select(tb_s.Text.Trim());//调用查询方法模糊查询专业
  85. if (li.Count <= )//未查到数据
  86. {
  87. }
  88. else//查到数据
  89. {
  90. int cou = ;//用于查到的为多条数据
  91. foreach (Subject ub in li)
  92. {
  93. if (li.Count == )//只查到一条数据
  94. {
  95. if (count == )//性别与年龄输入框都未输入内容
  96. {
  97. sql += " where SubjectCode =@c";
  98. sql2 += " where SubjectCode =@c";
  99. tj += " where SubjectCode =@c";
  100. }
  101. else
  102. {
  103. sql += " and SubjectCode =@c";
  104. sql2 += " and SubjectCode =@c";
  105. tj += " and SubjectCode =@c";
  106. }
  107. hs.Add("@c", ub.SubjectCode);
  108. cou++;
  109. count++;
  110. }
  111. else//查到多条数据
  112. {
  113. if (cou == )//第一次遍历
  114. {
  115. if (count == )
  116. {
  117. sql += " where (SubjectCode =@c";
  118. sql2 += " where (SubjectCode =@c";
  119. tj += " where (SubjectCode =@c";
  120. }
  121. else//性别与年龄输入框都未输入内容
  122. {
  123. sql += " and (SubjectCode =@c";
  124. sql2 += " and (SubjectCode =@c";
  125. tj += " and (SubjectCode =@c";
  126. }
  127. hs.Add("@c", ub.SubjectCode);
  128. cou++;
  129. }
  130. else
  131. {
  132. sql += " or SubjectCode =@d)";
  133. sql2 += " or SubjectCode =@d)";
  134. tj += " or SubjectCode =@d)";
  135. hs.Add("@d", ub.SubjectCode);
  136. }
  137. }
  138.  
  139. }
  140. }
  141. }
  142. }
  143.  
  144. Tsql方法

5.组合查询 按钮功能赋予

  1. void Button2_Click(object sender, EventArgs e)
  2. {
  3. string sql;//拼接查询前PageCount条数据的语句
  4. string sql2;//查询所有的语句
  5. string tj;
  6. int count;
  7. Tsql(out sql, out sql2,out tj,out count);
  8. Repeater1.DataSource = new StudentData().Select(sql, hs);//数据指向
  9. Repeater1.DataBind();
  10. Label2.Text = "";
  11. Label3.Text = MaxPageNumber(sql2,hs).ToString();//获取当前的最大页
  12. DropDownList2.Items.Clear();
  13. for (int i = ; i <= MaxPageNumber(sql2,hs); i++)//更新快捷跳转列表框
  14. {
  15. DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
  16. }
  17. }
  18.  
  19. 组合查询

6.分页代码

  1. void btn_next_Click(object sender, EventArgs e)
  2. {
  3. int pagec = Convert.ToInt32(Label2.Text) + ;//获取下一页为第几页
  4. string sql;//拼接查询前PageCount条数据的语句
  5. string sql2;//查询所有的语句
  6. string tj;
  7. int count;
  8. Tsql(out sql, out sql2, out tj, out count);
  9. if (pagec > MaxPageNumber(sql2,hs))//当前为最大页
  10. {
  11. return;
  12. }
  13. else
  14. {
  15. if(count>)//进行的是组合查询的下一页跳转
  16. {
  17. sql += " and Code not in(select top " + (PageCount * (pagec - )) + " Code from Student " + tj + ")";
  18. }
  19. else
  20. {
  21. sql += " where Code not in(select top " + (PageCount * (pagec - )) + " Code from Student " + tj + ")";
  22. }
  23. }
  24. Repeater1.DataSource = new StudentData().Select(sql, hs);//数据指向
  25. Repeater1.DataBind();
  26. Label2.Text = pagec.ToString();//更新当前页面
  27. DropDownList2.SelectedValue = pagec.ToString();
  28. }
  29.  
  30. 下一页
  1. void btn_prev_Click(object sender, EventArgs e)
  2. {
  3. int pagec = Convert.ToInt32(Label2.Text) - ;//获取上一页为第几页
  4. string sql;//拼接查询前PageCount条数据的语句
  5. string sql2;
  6. string tj;
  7. int count;
  8. Tsql(out sql, out sql2, out tj, out count);
  9. if (pagec <= )//当前为第一页
  10. {
  11. return;
  12. }
  13. if (count > )//进行的是组合查询的上一页跳转
  14. {
  15. sql += " and Code not in(select top " + (PageCount * (pagec - )) + " Code from Student " + tj + ")";
  16. }
  17. else
  18. {
  19. sql += " where Code not in(select top " + (PageCount * (pagec - )) + " Code from Student " + tj + ")";
  20. }
  21. List<Student> list = new StudentData().Select(sql, hs);//数据指向
  22. Repeater1.DataSource = list;
  23. Repeater1.DataBind();
  24. Label2.Text = pagec.ToString();//更新当前页面
  25. DropDownList2.SelectedValue = pagec.ToString();
  26. }
  27.  
  28. 上一页

上一页

  1. void btn_first_Click(object sender, EventArgs e)
  2. {
  3. string sql;
  4. string sql2;
  5. string tj;
  6. int count;
  7. Tsql(out sql, out sql2, out tj, out count);
  8. List<Student> list = new StudentData().Select(sql, hs);//数据指向
  9. Repeater1.DataSource = list;
  10. Repeater1.DataBind();
  11. Label2.Text = "";
  12. DropDownList2.SelectedValue = "";
  13. }
  14.  
  15. 跳转到第一页

首页

  1. void btn_end_Click(object sender, EventArgs e)
  2. {
  3. string sql;
  4. string sql2;
  5. string tj;
  6. int count;
  7. Tsql(out sql, out sql2, out tj, out count);
  8. if (count > )//进行的是组合查询的末页跳转
  9. {
  10. sql += " and Code not in(select top " + (PageCount * (MaxPageNumber(sql2,hs) - )) + " Code from Student " + tj + ")";
  11. }
  12. else
  13. {
  14. sql += " where Code not in(select top " + (PageCount * (MaxPageNumber(sql2, hs) - )) + " Code from Student " + tj + ")";
  15. }
  16. List<Student> list = new StudentData().Select(sql, hs);//数据指向
  17. Repeater1.DataSource = list;
  18. Repeater1.DataBind();
  19. Label2.Text = MaxPageNumber(sql2,hs).ToString();
  20. DropDownList2.SelectedValue = MaxPageNumber(sql2,hs).ToString();
  21. }
  22.  
  23. 最后一页跳转

末页

  1. void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. string sql;
  4. string sql2;
  5. string tj;
  6. int count;
  7. Tsql(out sql, out sql2, out tj, out count);
  8. if (count > )//进行的是组合查询的快捷跳转
  9. {
  10. sql += " and Code not in(select top " + (PageCount * (Convert.ToInt32(DropDownList2.SelectedValue) - )) + " Code from Student " + tj + ")";
  11. }
  12. else
  13. {
  14. sql += " where Code not in(select top " + (PageCount * (Convert.ToInt32(DropDownList2.SelectedValue) - )) + " Code from Student " + tj + ")";
  15. }
  16. Repeater1.DataSource = new StudentData().Select(sql, hs);//数据指向
  17. Repeater1.DataBind();
  18. Label2.Text = DropDownList2.SelectedValue;
  19. }
  20.  
  21. 快捷跳转

快捷跳转(跳至第...页)

WebForm 分页与组合查询的更多相关文章

  1. webform 分页、组合查询综合使用

    界面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx ...

  2. WebForm 分页、组合查询--2017年1月5日

    sql = "select * from Commodity"; hs = new Hashtable(); if (txt_name.Text.Trim() != "& ...

  3. Webform(Linq高级查、分页、组合查询)

    一.linq高级查 1.模糊查(包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r => ...

  4. ajax分页与组合查询配合使用

    使用纯HTML页与js.ajax.Linq实现分页与组合查询的配合使用 <body> <div id="top"><input type=" ...

  5. Webform(分页与组合查询配合使用)

    1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name) { List<Subject> ...

  6. Webform(分页、组合查询)

    一.分页 1.写查询方法: public List<Student> Select(int PageCount, int PageNumber) {//PageCount为每页显示条数,P ...

  7. WebForm之Linq组合查询

    组合查询 protected void Button1_Click(object sender, EventArgs e) { //默认查询所有,返回的是Table类型,转换成IQueryAble类型 ...

  8. Linq的分页与组合查询的配合使用

    1.首先使用Linq连接数据库,并扩展属性 public partial class User { public string SexStr { get { string end = "&l ...

  9. linq分页组合查询

    一.linq高级查 1.模糊查(字符串包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r = ...

随机推荐

  1. Dynamics CRM JS的调试的弊端解决办法

    说道CRMJS的调试的博客,之前已经有人写过.很简单,和平常网站JS的调试过程大致相同. 但是Dynamics 中JS调试最麻烦的莫过于出错之后需要修改代码了.因为随着JS代码的修改,伴随着需要保存和 ...

  2. WPF界面按钮美化

    在App.xaml里加入全局按钮样式 <Application x:Class="WpfButton.App" xmlns="http://schemas.micr ...

  3. noip模拟赛:部队[技巧?思想?]

    王国军总指挥——卡西乌斯准将决定重建情报局,需要从全国各地挑选有能力的士兵,选择的标准为A,B两种能力.对于每个候选士兵,如果存在另一名士兵的两项能力均大于等于他,那么他将被淘汰.(注意:若两名士兵两 ...

  4. vijos1004 博弈论

    一道挺简单的博弈论题 感觉自己也没有很规范的学过博弈论吧,就是偶尔刷到博弈论的题目,感受一下推导的过程,大概能领悟些什么 我们设2001.11.4必败,推上去,即2001.10.4和2001.11.3 ...

  5. Ruby on rails3

    Ruby on rails初体验(三)   继体验一和体验二中的内容,此节将体验二中最开始的目标来实现,体验二中已经将部门添加的部分添加到了公司的show页面,剩下的部分是将部门列表也添加到公司的显示 ...

  6. [google面试CTCI] 1-6.图像旋转问题

    [字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...

  7. Block formatting context

    不会JS中的OOP,你也太菜了吧!(第一篇)   一.你必须知道的 1) 字面量 2) 原型 3) 原型链 4) 构造函数 5) 稳妥对象(没有公共属性,而且其方法也不引用this的对象.稳妥对象适合 ...

  8. IOS7学习之路十(百度地图API环境搭建)

    百度地图官网的API开发教程链接:点击打开链接 我按照他的教程做的总出现"Apple Mach-O linker command failed with exit code 1"的 ...

  9. http概览

    http概览 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议.它可以使浏览器更加高效,使网络传输减少.它不仅 ...

  10. boost解析XML方法教程

    boost库在解析XML时具有良好的性能,可操作性也很强下地址有个简单的说明 http://blog.csdn.net/luopeiyuan1990/article/details/9445691 一 ...