WebForm 分頁與組合查詢

1.封裝實體類

2.寫查詢方法

//SubjectData類
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類/// <summary>/// 查詢方法/// </summary>/// <param name="tsql">SQL語句</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;}查詢方法

3.Page_Load部分,最大頁方法

int PageCount = 5; //每頁顯示條數Hashtable hs = new Hashtable();protected void Page_Load(object sender, EventArgs e){if(!IsPostBack){string tsql = "select top "+PageCount+" *from Student";//查詢前PageCount條數據//Repeater1數據源指向List<Student> list = new StudentData().Select(tsql,hs);Repeater1.DataSource = list;Repeater1.DataBind();Label2.Text = "1";//第一頁//獲取最大頁string sql = "select *from Student";Label3.Text = MaxPageNumber(sql,hs).ToString();for (int i = 1; i <= MaxPageNumber(sql,hs); i++)//給可快速跳轉列表框賦值
    {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);//查詢所有數據double de = list.Count / (PageCount * 1.0);int aa = Convert.ToInt32(Math.Ceiling(de));//取上限return aa;}獲取最大頁

4.根據組合查詢拼接語句方法

/// <summary>/// /// </summary>/// <param name="sql">拼接查詢前PageCount條數據的語句</param>/// <param name="sql2">查詢所有的語句</param>/// <param name="tj">用于分頁查詢與sql等拼接</param>/// <param name="count">判斷前幾項是否為空</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())){//判斷輸入的是男是女,其它輸入默認為未輸入內容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;//獲取當前時間的年try//確保輸入的是數字
            {int ag = Convert.ToInt32(tb_age.Text.Trim());int g = a - ag;DateTime d = Convert.ToDateTime(g.ToString() + "-1-1");if (DropDownList3.SelectedValue == ">=")//小于或等于您輸入的年齡,即大于或等于某個時間
                {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", d);}else//大于或等于您輸入的年齡,即小于或等于某個時間
                {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()))//判斷專業是否為空
        {List<Subject> li = new SubjectData().Select(tb_s.Text.Trim());//調用查詢方法模糊查詢專業if (li.Count <= 0)//未查到數據
            {}else//查到數據
            {int cou = 0;//用于查到的為多條數據foreach (Subject ub in li){if (li.Count == 1)//只查到一條數據
                    {if (count == 0)//性別與年齡輸入框都未輸入內容
                        {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//查到多條數據
                    {if (cou == 0)//第一次遍歷
                        {if (count == 0){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++;}else{sql += " or SubjectCode =@d)";sql2 += " or SubjectCode =@d)";tj += " or SubjectCode =@d)";hs.Add("@d", ub.SubjectCode);}}}}}}Tsql方法
View Code

5.組合查詢 按鈕功能賦予

void Button2_Click(object sender, EventArgs e){       string sql;//拼接查詢前PageCount條數據的語句string sql2;//查詢所有的語句string tj;int count;Tsql(out sql, out sql2,out tj,out count);Repeater1.DataSource = new StudentData().Select(sql, hs);//數據指向
        Repeater1.DataBind();Label2.Text = "1";Label3.Text = MaxPageNumber(sql2,hs).ToString();//獲取當前的最大頁
        DropDownList2.Items.Clear();for (int i = 1; i <= MaxPageNumber(sql2,hs); i++)//更新快捷跳轉列表框
        {DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));}}組合查詢
View Code

6.分頁代碼

void btn_next_Click(object sender, EventArgs e){int pagec = Convert.ToInt32(Label2.Text) + 1;//獲取下一頁為第幾頁string sql;//拼接查詢前PageCount條數據的語句string sql2;//查詢所有的語句string tj;int count;Tsql(out sql, out sql2, out tj, out count);if (pagec > MaxPageNumber(sql2,hs))//當前為最大頁
        {return;}else{if(count>0)//進行的是組合查詢的下一頁跳轉
           {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);//數據指向
        Repeater1.DataBind();Label2.Text = pagec.ToString();//更新當前頁面DropDownList2.SelectedValue = pagec.ToString();}下一頁
View Code
void btn_prev_Click(object sender, EventArgs e){int pagec = Convert.ToInt32(Label2.Text) - 1;//獲取上一頁為第幾頁string sql;//拼接查詢前PageCount條數據的語句string sql2;string tj;int count;Tsql(out sql, out sql2, out tj, out count);if (pagec <= 0)//當前為第一頁
        {return;}if (count > 0)//進行的是組合查詢的上一頁跳轉
        {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);//數據指向Repeater1.DataSource = list;Repeater1.DataBind();Label2.Text = pagec.ToString();//更新當前頁面DropDownList2.SelectedValue = pagec.ToString();}上一頁
上一頁
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);//數據指向Repeater1.DataSource = list;Repeater1.DataBind();Label2.Text = "1";DropDownList2.SelectedValue = "1";}跳轉到第一頁
首頁
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)//進行的是組合查詢的末頁跳轉
        {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);//數據指向Repeater1.DataSource = list;Repeater1.DataBind();Label2.Text = MaxPageNumber(sql2,hs).ToString();DropDownList2.SelectedValue = MaxPageNumber(sql2,hs).ToString();}最后一頁跳轉
末頁
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)//進行的是組合查詢的快捷跳轉
        {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);//數據指向
        Repeater1.DataBind();Label2.Text = DropDownList2.SelectedValue;}快捷跳轉
快捷跳轉(跳至第...頁)

?

轉載于:https://www.cnblogs.com/hongsen3/p/5994881.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457086.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457086.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457086.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux如何輸出當前時間,如何在linux下輸出當前時間

用localtime可直接分解出年月日時分秒QUOTE:struct tm *ptm;long ts;int y,m,d,h,n,s;ts time(NULL);ptm localtime(&ts);y ptm->tm_year1900; //年m ptm->tm_mon1; //月d ptm->tm_mday; //日h ptm->tm_hour; //時n ptm->tm_min; //分s ptm->tm_…

node.js簡單爬蟲

這里假設你已經安裝好node.js和npm&#xff0c;如果沒有安裝&#xff0c;請參閱其他教程安裝。 配置首先是來配置package.json文件&#xff0c;這里使用express,request和cheerio。package.json如下&#xff1a; {"name": "node-scrape","version&quo…

Day11-遞歸性能測試

import time time.clock() sum 0 for i in range (1000000000):sumi print(time.clock()) 慎用 慎用 慎用

關于在新建的package中用SetContentView()函數時無法找到已創建的R.layout的布局文件的的問題的解決辦法

問題描述如下&#xff1a; 解決途徑&#xff1a;是在導入包的過程中&#xff0c;錯誤的將系統自動將Android.R這個包導入最終導致用setContenView()加載布局時只能顯示系統自帶的布局&#xff0c;無法顯示自己已經創建的布局。只需將相應活動中導入的Android.R包刪除&#xff0…

Struts2入門(二)——配置攔截器

一、前言 之前便了解過&#xff0c;Struts 2的核心控制器是一個Filter過濾器&#xff0c;負責攔截所有的用戶請求&#xff0c;當用戶請求發送過來時&#xff0c;會去檢測struts.xml是否存在這個action&#xff0c;如果存在&#xff0c;服務器便會自動幫我們跳轉到指定的處理類中…

linux固態機械分區嗎,不再疑惑!實測數據后才知道固態硬盤究竟要不要分區

不再疑惑&#xff01;實測數據后才知道固態硬盤究竟要不要分區2019-12-10 20:52:00162點贊594收藏177評論前幾年的固態硬盤價格昂貴&#xff0c;一般用戶會選擇128G或256G的固態作為系統盤&#xff0c;由于單盤空間不大&#xff0c;一般都會配合機械硬盤使用&#xff0c;無需考…

關于無法加載已創建的布局文件的問題的解決方案以及已布局在對應的R文件中未生成相應ID的問題的解決

先來說下創建后的Layout布局文件在對應的R文件中不能生成相應的ID問題&#xff0c;一般情況下之所以出現這種問題是應為自己的res文件中有錯誤的文件&#xff1a;對應的是錯誤的文件格式名稱&#xff0c;以及錯誤的文件內容等。博主就遇到過為drawable文件起了一個非法的名稱&a…

安卓手機的后門控制工具SPADE

SPADE&#xff0c;一款安卓手機的后門控制工具&#xff0c;安全研究人員可以以此了解和研究安卓后門原理。 首先&#xff0c;我們從網站www.apk4fun.com下載apk文件&#xff0c;如ccleaner。然后&#xff0c;我們安裝spade git clone https://github.com/suraj-root/spade.git …

Day12-date time

import datetimedatetime比time高級了不少&#xff0c;可以理解為datetime基于time進行了封裝&#xff0c;提供了&#xff0c; 更為實用的函數&#xff0c;并且datetime模塊的接口更直觀更容易調用模塊中的類&#xff1a; datetime 同時又時間和日期 imedelta 主…

MySQL案例-open too many files,MyISAM與partition

-------------------------------------------------------------------------------------------------短文---------------------------------------------------------------------------------------------------------------長話短說~現象: error log中批量刷錯誤日志, 形…

關于異常:HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection的解決辦法

<span style"font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style"font-size:18px;">今天在寫一個app時&#xff0c;當實現從網絡上獲取圖片資源&#xff0c;發送HTTPURLConnection的時候拋出這樣…

linux網卡有很多error,教你設置win7系統虛擬機安裝linux提示network error的解決方法...

很多朋友在使用電腦的過程中&#xff0c;會發現win7系統虛擬機安裝linux提示network error的現象&#xff0c;當遇到win7系統虛擬機安裝linux提示network error的問題&#xff0c;我們要怎么解決呢&#xff1f;如今還有很多用戶不知道如何處理win7系統虛擬機安裝linux提示netwo…

codevs2171 棋盤覆蓋

題目描述 Description給出一張n*n(n<100)的國際象棋棋盤&#xff0c;其中被刪除了一些點&#xff0c;問可以使用多少1*2的多米諾骨牌進行掩蓋。 輸入描述 Input Description第一行為n&#xff0c;m&#xff08;表示有m個刪除的格子&#xff09;第二行到m1行為x,y&#xff0c…

Day13-日歷模塊

import calendar日歷模塊 #使用#返回制定歿年某月日歷 print(calendar.month(2019,3)) #返回指定年份的日歷 print(calendar.calendar(2019)) #判斷閏年返回True 或者Flase print(calendar.isleap(2000)) #返回某個月的weekd的第一天和這個月所有的天數 print(calendar.monthra…

關于eclipse項目紅色感嘆號的解決辦法

在網上找到了解決辦法&#xff0c;詳見&#xff1a;http://jingyan.baidu.com/article/ea24bc3986f7b0da62b33188.html

linux模擬網絡延遲,使用Nistnet搭建網絡延遲模擬設備 (network delay simulator)

mknod /dev/hitbox c 62 0mknod /dev/nistnet c 62 1chown root /dev/hitboxchown root /dev/nistnetmknod /dev/mungebox c 63 0chown root /dev/mungeboxmknod /dev/spybox c 64 0chown root /dev/spyboxmodprobe nistnet可以將這個放到/etc/rc.local中&#xff0c;以便重啟后…

MyBatis - MyBatis Generator 生成的example 如何使用 and or 簡單混合查詢

簡單介紹&#xff1a; Criteria&#xff0c;包含一個Cretiron的集合,每一個Criteria對象內包含的Cretiron之間是由AND連接的,是邏輯與的關系。oredCriteria&#xff0c;Example內有一個成員叫oredCriteria,是Criteria的集合,就想其名字所預示的一樣&#xff0c;這個集合中的Cri…

將本地Blog部署到GitHub上,有自己的博客頁面!

前言 上一篇文章我們已經把本地的hexo環境搭建好了&#xff0c;并且在本地成功預覽&#xff0c;但是本地預覽也意味著自己的博文只能自己看的到&#xff0c;其他人根本看不到&#xff0c;這篇文章將接上文說一說如何把本地Blog部署到GitHub上&#xff0c;好讓小伙伴可以來訪問我…

Linux下安裝配置JDK

本人使用的VM虛擬機&#xff0c;在VM上安裝了Linux&#xff0c;版本是CentOS-6.7-i386-bin-DVD1.iso。 一、下載JDK 在進入JDK官網&#xff0c;找到要下載的JDK版本&#xff0c;將下載地址復制下來&#xff0c;放到迅雷中下載&#xff0c;我下載的是&#xff1a;http://downloa…