码迷,mamicode.com
首页 > 其他好文 > 详细

组合查询分页代码

时间:2017-05-26 11:57:57      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:student   数据   lan   hid   last   nbsp   reader   its   方法   

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        名称:<asp:TextBox ID="name" runat="server"></asp:TextBox>
        油耗:<asp:DropDownList ID="oil" runat="server">
            <asp:ListItem Text="大于" Value=">"></asp:ListItem>
            <asp:ListItem Text="小于" Value="<"></asp:ListItem>
            <asp:ListItem Text="等于" Value="="></asp:ListItem>
            <asp:ListItem Text="大于等于" Value=">="></asp:ListItem>
            <asp:ListItem Text="小于等于" Value="<="></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="oiltext" runat="server"></asp:TextBox>
        价格:<asp:DropDownList ID="price" runat="server">
            <asp:ListItem Text="20万至30万" Value="price>=20 and price<=30"></asp:ListItem>
            <asp:ListItem Text="30万至40万" Value="price>=30 and price<=40"></asp:ListItem>
            <asp:ListItem Text="大于40万" Value="price>=40"></asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="查询" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <table style="width: 100%; background-color: blue; text-align: center;">
            <tr style="color: white;">
                <td>编号</td>
                <td>名称</td>
                <td>油耗</td>
                <td>马力</td>
                <td>排量</td>
                <td>价格</td>
            </tr>
            <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <tr style="background-color: white;">
                        <td><%#Eval("Ids") %></td>
                        <td><%#Eval("Name") %></td>
                        <td><%#Eval("Oil") %></td>
                        <td><%#Eval("Powers") %></td>
                        <td><%#Eval("Exhaust") %></td>
                        <td><%#Eval("Price") %></td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
        第[<asp:Label ID="nowpage" runat="server" Text="1"></asp:Label>]页&nbsp;&nbsp;
        共[<asp:Label ID="sumpage" runat="server" Text="1"></asp:Label>]页 &nbsp;&nbsp;
        <asp:Button ID="bt_first" runat="server" Text="首页" />
        <asp:Button ID="bt_up" runat="server" Text="上一页" />
        <asp:Button ID="bt_next" runat="server" Text="下一页" />
        <asp:Button ID="bt_last" runat="server" Text="尾页" />

        &nbsp;&nbsp;
        <asp:DropDownList ID="Dr_page" runat="server"></asp:DropDownList>
        <asp:Button ID="bt_go" runat="server" Text="跳转" />

    </form>
</body>
</html>
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Car 的摘要说明
/// </summary>
public class Car
{
    public int Ids { get; set; }
    public string Name { get; set; }
    public decimal Oil { get; set; }
    public int Powers { get; set; }
    public int Exhaust { get; set; }
    public decimal Price { get; set; }
}
实体类
技术分享
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// CarData 的摘要说明
/// </summary>
public class CarData
{
    SqlConnection conn = null;
    SqlCommand cmd = null;
    public CarData()
    {
        conn = new SqlConnection("server=.;database=student;user=sa;pwd=123456");
        cmd = conn.CreateCommand();
    }
    //查询全部信息
    public List<Car> SelectAll()
    {
        List<Car> clist = new List<Car>();
        cmd.CommandText = "select*from Car";
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Car ca = new Car();

            ca.Ids = Convert.ToInt32(dr["ids"]);
            ca.Name = dr["name"].ToString();
            ca.Oil = Convert.ToDecimal(dr["oil"]);
            ca.Powers = Convert.ToInt32(dr["powers"]);
            ca.Exhaust = Convert.ToInt32(dr["exhaust"]);
            ca.Price = Convert.ToDecimal(dr["price"]);

            clist.Add(ca);
        }
        conn.Close();
        return clist;
    }
    //查询最大条数
    public int SelectCount()
    {
        int count = 0;
        cmd.CommandText = "select count(*)from Car";
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            dr.Read();
            count = Convert.ToInt32(dr[0]);
        }
        conn.Close();
        return count;
    }

    //查询最大条数
    public int SelectCount(string tsa,Hashtable hh)
    {
        int count = 0;
        cmd.CommandText = tsa;
        cmd.Parameters.Clear();
        foreach (string a in hh.Keys)
        {
            cmd.Parameters.AddWithValue(a, hh[a]);
        }
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            dr.Read();
            count = Convert.ToInt32(dr[0]);
        }
        conn.Close();
        return count;
    }
    //条件查询
    public List<Car> SelectAll(string tsql ,Hashtable hh)
    {
        
        List<Car> clist = new List<Car>();
        cmd.CommandText = tsql;
        cmd.Parameters.Clear();
        foreach (string a in hh.Keys)
        {
            cmd.Parameters.AddWithValue(a,hh[a]);
        }
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Car ca = new Car();

            ca.Ids = Convert.ToInt32(dr["ids"]);
            ca.Name = dr["name"].ToString();
            ca.Oil = Convert.ToDecimal(dr["oil"]);
            ca.Powers = Convert.ToInt32(dr["powers"]);
            ca.Exhaust = Convert.ToInt32(dr["exhaust"]);
            ca.Price = Convert.ToDecimal(dr["price"]);

            clist.Add(ca);
        }
        conn.Close();
        return clist;
    }
    //跳页查询
    public List<Car> SelectAll(int num, int page)
    {

        List<Car> clist = new List<Car>();
        cmd.CommandText = "select top " + num + " *from Car where ids not in(select top " + num * (page - 1) + " ids from Car)";
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Car ca = new Car();

            ca.Ids = Convert.ToInt32(dr["ids"]);
            ca.Name = dr["name"].ToString();
            ca.Oil = Convert.ToDecimal(dr["oil"]);
            ca.Powers = Convert.ToInt32(dr["powers"]);
            ca.Exhaust = Convert.ToInt32(dr["exhaust"]);
            ca.Price = Convert.ToDecimal(dr["price"]);

            clist.Add(ca);
        }
        conn.Close();
        return clist;
    }
}
数据访问类
技术分享
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    int num = 5;
    protected void Page_Load(object sender, EventArgs e)
    {
        int page = Convert.ToInt32(nowpage.Text);
        if (!IsPostBack)
        {
            Repeater1.DataSource = End(1);
            Repeater1.DataBind();

            sumpage.Text = Maxpage().ToString();
                      
            if (page == Maxpage())
            {
                bt_first.Enabled = false;
                bt_up.Enabled = false;
                bt_next.Enabled = false;
                bt_last.Enabled = false;

            }
            for (int i = 1; i <= Maxpage(); i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                Dr_page.Items.Add(li);
            }
        }
        Button1.Click += Button1_Click;
        bt_last.Click += bt_last_Click;
        bt_next.Click += bt_next_Click;
        bt_up.Click += bt_up_Click;
        bt_first.Click += bt_first_Click;
        bt_go.Click += bt_go_Click;

    }

    void bt_go_Click(object sender, EventArgs e)
    {
        bt_first.Enabled = true;
        bt_up.Enabled = true;
        bt_next.Enabled = true;
        bt_last.Enabled = true;
        int a = Convert.ToInt32(Dr_page.SelectedValue);
        Repeater1.DataSource = End(a);
        Repeater1.DataBind();
        //显示当前页数
        nowpage.Text = a.ToString();
        if (a == 1)
        {
            bt_first.Enabled = false;
            bt_up.Enabled = false;
            return;
        }
        if (a == Maxpage())
        {
            bt_next.Enabled = false;
            bt_last.Enabled = false;
            return;
        }
    }

    void bt_first_Click(object sender, EventArgs e)
    {
        bt_last.Enabled = true;
        bt_next.Enabled = true;
        Repeater1.DataSource = End(1);
        Repeater1.DataBind();
        //显示当前页数
        nowpage.Text = "1";
        bt_up.Enabled = false;
    }

    void bt_up_Click(object sender, EventArgs e)
    {
        //获取下一页查询页数
        int page = Convert.ToInt32(nowpage.Text);
        int uppage = page - 1;        
        if (uppage < 1)
        {
            return;
        }
        if (page == 1)
        {
            bt_first.Enabled = false;
            bt_up.Enabled = false;
        }
        //执行查询语句
        Repeater1.DataSource = End(uppage);
        Repeater1.DataBind();
        //显示当前页数
        nowpage.Text = uppage.ToString();
        //上一页按钮不可用

        bt_next.Enabled = true;
        bt_last.Enabled = true;
    }

    void bt_next_Click(object sender, EventArgs e)
    {
        //上一页按钮可用
        bt_first.Enabled = true;
        bt_up.Enabled = true;
        //获取下一页查询页数
        int page = Convert.ToInt32(nowpage.Text);
        int nextpage = page + 1;
        //下一页按钮不可用
        if (nextpage > Maxpage())
        {
            bt_last.Enabled = false;
            bt_next.Enabled = false;
            return;
        }
        //执行查询语句
        Repeater1.DataSource = End(nextpage);
        Repeater1.DataBind();
        //显示当前页数
        nowpage.Text = nextpage.ToString();
    }

    void bt_last_Click(object sender, EventArgs e)
    {
        bt_first.Enabled = true;
        bt_up.Enabled = true;

        Repeater1.DataSource = End(Maxpage());
        Repeater1.DataBind();
        //显示当前页数
        nowpage.Text = Maxpage().ToString();
        bt_next.Enabled = false;
    }
    //总页数方法
    public int Maxpage()
    {
        int end = 0;
        int item = Count();
        decimal a = Convert.ToDecimal(item) / num;
        end = Convert.ToInt32(Math.Ceiling(a));
        return end;
    }



    void Button1_Click(object sender, EventArgs e)
    {
        int count = 0;
        int page = Convert.ToInt32(nowpage.Text);
        string tsql = "select top " + num + " *from Car where ids not in(select top " + num * (page - 1) + " ids from Car ";
        string tasp = " ";
        Hashtable hs = new Hashtable();
        if (name.Text.Length > 0)
        {
            tsql += " where name like @a";
            tasp += " )and name like @a";
            hs.Add("@a", "%" + name.Text.Trim() + "%");
            count++;

        }
        if (oiltext.Text.Length > 0)
        {
            if (count > 0)
            {
                tsql += " and oil " + oil.SelectedValue + "@b";
                tasp += " and oil" + oil.SelectedValue + "@b";

            }
            else
            {
                tsql += " where oil " + oil.SelectedValue + "@b";
                tasp += " )and oil" + oil.SelectedValue + "@b";
            }
            hs.Add("@b", oiltext.Text.Trim());
            count++;
        }
        if (price.SelectedValue != null)
        {
            if (count > 0)
            {
                tsql += " and " + price.SelectedValue;
                tasp += " and " + price.SelectedValue;
            }
            else
            {
                tsql += " where " + price.SelectedValue;
                tasp += " )and " + price.SelectedValue;
            }
            count++;
        }
        else
        {
            tsql += ")";
        }
        tsql = tsql + tasp;
        Label1.Text = tsql;
        Repeater1.DataSource = new CarData().SelectAll(tsql, hs);
        Repeater1.DataBind();
    }
    //总查询方法
    public List<Car> End(int page)
    {

        int count = 0;
        string tsql = "select top " + num + " *from Car where ids not in(select top " + num * (page - 1) + " ids from Car ";
        string tasp = " ";
        Hashtable hs = new Hashtable();
        if (name.Text.Length > 0)
        {
            tsql += " where name like @a";
            tasp += " )and name like @a";
            hs.Add("@a", "%" + name.Text.Trim() + "%");
            count++;
        }
        if (oiltext.Text.Length > 0)
        {
            if (count > 0)
            {
                tsql += " and oil " + oil.SelectedValue + "@b";
                tasp += " and oil" + oil.SelectedValue + "@b";

            }
            else
            {
                tsql += " where oil " + oil.SelectedValue + "@b";
                tasp += " )and oil" + oil.SelectedValue + "@b";
            }
            hs.Add("@b", oiltext.Text.Trim());
            count++;
        }
        if (price.SelectedValue != null)
        {
            if (count > 0)
            {
                tsql += " and " + price.SelectedValue;
                tasp += " and " + price.SelectedValue;
            }
            else
            {
                tsql += " where " + price.SelectedValue;
                tasp += " )and " + price.SelectedValue;
            }
            count++;
        }
        else
        {
            tsql += ")";
        }
        tsql = tsql + tasp;
        Label1.Text = tsql;

        List<Car> clist = new CarData().SelectAll(tsql, hs);
        return clist;
    }
    //查询最大页数
    public int Count()
    {
        int end = 0;
        int count = 0;
        string tsql = "select count(*) from Car ";
        Hashtable hs = new Hashtable();
        if (name.Text.Length > 0)
        {
            tsql += " where name like @a";

            hs.Add("@a", "%" + name.Text.Trim() + "%");
            count++;
        }
        if (oiltext.Text.Length > 0)
        {
            if (count > 0)
            {
                tsql += " and oil " + oil.SelectedValue + "@b";
            }
            else
            {
                tsql += " where oil " + oil.SelectedValue + "@b";
            }
            hs.Add("@b", oiltext.Text.Trim());
            count++;
        }
        if (price.SelectedValue != null)
        {
            if (count > 0)
            {
                tsql += " and " + price.SelectedValue;
            }
            else
            {
                tsql += " where " + price.SelectedValue;
            }
            count++;
        }
        end = new CarData().SelectCount(tsql, hs);
        return end;
    }
}
数据访问层

 

组合查询分页代码

标签:student   数据   lan   hid   last   nbsp   reader   its   方法   

原文地址:http://www.cnblogs.com/hclyz/p/6907607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!