WebForm 分页与组合查询
生活随笔
收集整理的這篇文章主要介紹了
WebForm 分页与组合查询
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.封裝實(shí)體類(lèi)
2.寫(xiě)查詢(xún)方法
//SubjectData類(lèi) public List<Subject> Select(string name){List<Subject> list = new List<Subject>();cmd.CommandText = "select *from Subject where SubjectName like @a ";cmd.Parameters.Clear();cmd.Parameters.Add("@a","%"+name+"%");conn.Open();SqlDataReader dr = cmd.ExecuteReader();if (dr.HasRows){while (dr.Read()){Subject s = new Subject();s.SubjectCode = dr[0].ToString();s.SubjectName = dr[1].ToString();list.Add(s);}}conn.Close();return list;} //StudentData類(lèi)/// <summary>/// 查詢(xún)方法/// </summary>/// <param name="tsql">SQL語(yǔ)句</param>/// <param name="hh">哈希表</param>/// <returns></returns>public List<Student> Select(string tsql,Hashtable hh){List<Student> list = new List<Student>();cmd.CommandText = tsql;cmd.Parameters.Clear();foreach( string s in hh.Keys){cmd.Parameters.Add(s,hh[s]);}conn.Open();SqlDataReader dr = cmd.ExecuteReader();if (dr.HasRows){while (dr.Read()){Student s = new Student();s.Code = dr[0].ToString();s.Name = dr[1].ToString();s.Sex = Convert.ToBoolean(dr[2]);s.Birthday = Convert.ToDateTime(dr[3]);s.SubjectCode = dr[4].ToString();s.Nation = dr[5].ToString();list.Add(s);}}conn.Close();return list;}查詢(xún)方法3.Page_Load部分,最大頁(yè)方法
int PageCount = 5; //每頁(yè)顯示條數(shù)Hashtable hs = new Hashtable();protected void Page_Load(object sender, EventArgs e){if(!IsPostBack){string tsql = "select top "+PageCount+" *from Student";//查詢(xún)前PageCount條數(shù)據(jù)//Repeater1數(shù)據(jù)源指向List<Student> list = new StudentData().Select(tsql,hs);Repeater1.DataSource = list;Repeater1.DataBind();Label2.Text = "1";//第一頁(yè)//獲取最大頁(yè)string sql = "select *from Student";Label3.Text = MaxPageNumber(sql,hs).ToString();for (int i = 1; i <= MaxPageNumber(sql,hs); i++)//給可快速跳轉(zhuǎn)列表框賦值 {DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));} } }Page_Load public int MaxPageNumber(string sql, Hashtable hs){List<Student> list = new StudentData().Select(sql, hs);//查詢(xún)所有數(shù)據(jù)double de = list.Count / (PageCount * 1.0);int aa = Convert.ToInt32(Math.Ceiling(de));//取上限return aa;}獲取最大頁(yè)4.根據(jù)組合查詢(xún)拼接語(yǔ)句方法
/// <summary>/// /// </summary>/// <param name="sql">拼接查詢(xún)前PageCount條數(shù)據(jù)的語(yǔ)句</param>/// <param name="sql2">查詢(xún)所有的語(yǔ)句</param>/// <param name="tj">用于分頁(yè)查詢(xún)與sql等拼接</param>/// <param name="count">判斷前幾項(xiàng)是否為空</param>private void Tsql(out string sql, out string sql2,out string tj,out int count){count = 0;sql = "select top " + PageCount + " *from Student";sql2 = "select *from Student";tj = "";//性別不為空if (!string.IsNullOrEmpty(tb_sex.Text.Trim())){//判斷輸入的是男是女,其它輸入默認(rèn)為未輸入內(nèi)容if (tb_sex.Text.Trim() == "男"){sql += " where Sex = @a";sql2 += " where Sex = @a";tj += " where Sex = @a";hs.Add("@a", "true");count++;}else if (tb_sex.Text.Trim() == "女"){sql += " where Sex = @a";sql2 += " where Sex = @a";tj += " where Sex = @a";hs.Add("@a", "false");count++;}}//年齡不為空if (!string.IsNullOrEmpty(tb_age.Text.Trim())){int a = DateTime.Now.Year;//獲取當(dāng)前時(shí)間的年try//確保輸入的是數(shù)字 {int ag = Convert.ToInt32(tb_age.Text.Trim());int g = a - ag;DateTime d = Convert.ToDateTime(g.ToString() + "-1-1");if (DropDownList3.SelectedValue == ">=")//小于或等于您輸入的年齡,即大于或等于某個(gè)時(shí)間 {if (count == 0)//前面的一項(xiàng)未輸入(性別) {sql += " where Birthday " + DropDownList3.SelectedValue + "@b";sql2 += " where Birthday " + DropDownList3.SelectedValue + "@b";tj += " where Birthday " + DropDownList3.SelectedValue + "@b";}else{sql += " and Birthday " + DropDownList3.SelectedValue + "@b";sql2 += " and Birthday " + DropDownList3.SelectedValue + "@b";tj += " and Birthday " + DropDownList3.SelectedValue + "@b";}hs.Add("@b", d);}else//大于或等于您輸入的年齡,即小于或等于某個(gè)時(shí)間 {DateTime dd = Convert.ToDateTime(g.ToString() + "-12-31");if (count == 0){sql += " where Birthday " + DropDownList3.SelectedValue + "@b";sql2 += " where Birthday " + DropDownList3.SelectedValue + "@b";tj += " where Birthday " + DropDownList3.SelectedValue + "@b";}else{sql += " and Birthday " + DropDownList3.SelectedValue + "@b";sql2 += " and Birthday " + DropDownList3.SelectedValue + "@b";tj += " and Birthday " + DropDownList3.SelectedValue + "@b";}hs.Add("@b", dd);}count++;}catch{}}if (!string.IsNullOrEmpty(tb_s.Text.Trim()))//判斷專(zhuān)業(yè)是否為空 {List<Subject> li = new SubjectData().Select(tb_s.Text.Trim());//調(diào)用查詢(xún)方法模糊查詢(xún)專(zhuān)業(yè)if (li.Count <= 0)//未查到數(shù)據(jù) {}else//查到數(shù)據(jù) {int cou = 0;//用于查到的為多條數(shù)據(jù)foreach (Subject ub in li){if (li.Count == 1)//只查到一條數(shù)據(jù) {if (count == 0)//性別與年齡輸入框都未輸入內(nèi)容 {sql += " where SubjectCode =@c";sql2 += " where SubjectCode =@c";tj += " where SubjectCode =@c";}else{sql += " and SubjectCode =@c";sql2 += " and SubjectCode =@c";tj += " and SubjectCode =@c";}hs.Add("@c", ub.SubjectCode);cou++;count++;}else//查到多條數(shù)據(jù) {if (cou == 0)//第一次遍歷 {if (count == 0){sql += " where (SubjectCode =@c";sql2 += " where (SubjectCode =@c";tj += " where (SubjectCode =@c";}else//性別與年齡輸入框都未輸入內(nèi)容 {sql += " and (SubjectCode =@c";sql2 += " and (SubjectCode =@c";tj += " and (SubjectCode =@c";}hs.Add("@c", ub.SubjectCode);cou++;}else{sql += " or SubjectCode =@d)";sql2 += " or SubjectCode =@d)";tj += " or SubjectCode =@d)";hs.Add("@d", ub.SubjectCode);}}}}}}Tsql方法 View Code5.組合查詢(xún) 按鈕功能賦予
void Button2_Click(object sender, EventArgs e){ string sql;//拼接查詢(xún)前PageCount條數(shù)據(jù)的語(yǔ)句string sql2;//查詢(xún)所有的語(yǔ)句string tj;int count;Tsql(out sql, out sql2,out tj,out count);Repeater1.DataSource = new StudentData().Select(sql, hs);//數(shù)據(jù)指向 Repeater1.DataBind();Label2.Text = "1";Label3.Text = MaxPageNumber(sql2,hs).ToString();//獲取當(dāng)前的最大頁(yè) DropDownList2.Items.Clear();for (int i = 1; i <= MaxPageNumber(sql2,hs); i++)//更新快捷跳轉(zhuǎn)列表框 {DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));}}組合查詢(xún) View Code6.分頁(yè)代碼
void btn_next_Click(object sender, EventArgs e){int pagec = Convert.ToInt32(Label2.Text) + 1;//獲取下一頁(yè)為第幾頁(yè)string sql;//拼接查詢(xún)前PageCount條數(shù)據(jù)的語(yǔ)句string sql2;//查詢(xún)所有的語(yǔ)句string tj;int count;Tsql(out sql, out sql2, out tj, out count);if (pagec > MaxPageNumber(sql2,hs))//當(dāng)前為最大頁(yè) {return;}else{if(count>0)//進(jìn)行的是組合查詢(xún)的下一頁(yè)跳轉(zhuǎn) {sql += " and Code not in(select top " + (PageCount * (pagec - 1)) + " Code from Student " + tj + ")";}else{sql += " where Code not in(select top " + (PageCount * (pagec - 1)) + " Code from Student " + tj + ")";}}Repeater1.DataSource = new StudentData().Select(sql, hs);//數(shù)據(jù)指向 Repeater1.DataBind();Label2.Text = pagec.ToString();//更新當(dāng)前頁(yè)面DropDownList2.SelectedValue = pagec.ToString();}下一頁(yè) View Code void btn_prev_Click(object sender, EventArgs e){int pagec = Convert.ToInt32(Label2.Text) - 1;//獲取上一頁(yè)為第幾頁(yè)string sql;//拼接查詢(xún)前PageCount條數(shù)據(jù)的語(yǔ)句string sql2;string tj;int count;Tsql(out sql, out sql2, out tj, out count);if (pagec <= 0)//當(dāng)前為第一頁(yè) {return;}if (count > 0)//進(jìn)行的是組合查詢(xún)的上一頁(yè)跳轉(zhuǎn) {sql += " and Code not in(select top " + (PageCount * (pagec - 1)) + " Code from Student " + tj + ")";}else{sql += " where Code not in(select top " + (PageCount * (pagec - 1)) + " Code from Student " + tj + ")";}List<Student> list = new StudentData().Select(sql, hs);//數(shù)據(jù)指向Repeater1.DataSource = list;Repeater1.DataBind();Label2.Text = pagec.ToString();//更新當(dāng)前頁(yè)面DropDownList2.SelectedValue = pagec.ToString();}上一頁(yè) 上一頁(yè) void btn_first_Click(object sender, EventArgs e){string sql;string sql2;string tj;int count;Tsql(out sql, out sql2, out tj, out count);List<Student> list = new StudentData().Select(sql, hs);//數(shù)據(jù)指向Repeater1.DataSource = list;Repeater1.DataBind();Label2.Text = "1";DropDownList2.SelectedValue = "1";}跳轉(zhuǎn)到第一頁(yè) 首頁(yè) void btn_end_Click(object sender, EventArgs e){string sql;string sql2;string tj;int count;Tsql(out sql, out sql2, out tj, out count);if (count > 0)//進(jìn)行的是組合查詢(xún)的末頁(yè)跳轉(zhuǎn) {sql += " and Code not in(select top " + (PageCount * (MaxPageNumber(sql2,hs) - 1)) + " Code from Student " + tj + ")";}else{sql += " where Code not in(select top " + (PageCount * (MaxPageNumber(sql2, hs) - 1)) + " Code from Student " + tj + ")";}List<Student> list = new StudentData().Select(sql, hs);//數(shù)據(jù)指向Repeater1.DataSource = list;Repeater1.DataBind();Label2.Text = MaxPageNumber(sql2,hs).ToString();DropDownList2.SelectedValue = MaxPageNumber(sql2,hs).ToString();}最后一頁(yè)跳轉(zhuǎn) 末頁(yè) void DropDownList2_SelectedIndexChanged(object sender, EventArgs e){string sql;string sql2;string tj;int count;Tsql(out sql, out sql2, out tj, out count);if (count > 0)//進(jìn)行的是組合查詢(xún)的快捷跳轉(zhuǎn) {sql += " and Code not in(select top " + (PageCount * (Convert.ToInt32(DropDownList2.SelectedValue) - 1)) + " Code from Student " + tj + ")";}else{sql += " where Code not in(select top " + (PageCount * (Convert.ToInt32(DropDownList2.SelectedValue) - 1)) + " Code from Student " + tj + ")";}Repeater1.DataSource = new StudentData().Select(sql, hs);//數(shù)據(jù)指向 Repeater1.DataBind();Label2.Text = DropDownList2.SelectedValue;}快捷跳轉(zhuǎn) 快捷跳轉(zhuǎn)(跳至第...頁(yè))?
轉(zhuǎn)載于:https://www.cnblogs.com/hongsen3/p/5994881.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的WebForm 分页与组合查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: WinForm 清空界面控件值的小技巧
- 下一篇: 葡萄城报表V11 SP2新版本震撼发布!