组合查询分页代码

<%@ 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;
    }
}

数据访问层

时间: 2024-02-19 20:16:44

组合查询分页代码的相关文章

phpcms v9后台多表查询分页代码

phpcms v9里面自带的listinfo分页函数蛮好用的,可惜啊.不支持多表查询并分页. 看了一下前台模板层支持get标签,支持多表查询,支持分页.刚好可以把这个功能搬到后台来使用. 我们现在对get_model.class.php进行改造使他能支持多表查询并分页. <?php defined('IN_PHPCMS') or exit('No permission resources.'); pc_base::load_sys_class('model', '', 0); class get

组合查询 分页

分页: 当前第<asp:Label ID="Label1" runat="server" Text=""></asp:Label>页 共<asp:Label ID="Label2" runat="server" Text=""></asp:Label>页 <asp:LinkButton ID="LinkButton1&quo

eaeyui-combobox实现组合查询(即实现多个值得搜索)

2015年9月1日,今天要实现下拉框的组合查询功能,即可以再下拉框中选择多个值,输入框中每个值之间有逗号隔开,传到后台,由split函数将其分割开,组合成数组,在由sql查询. 实现的效果是: 当时在网上查了一下,有很多种方法,easyui-combobox,select2,multiple,tagsInput,由于我的前段使用easyui来实现功能的,所以选择了easyui-combobox来实现此功能.(主要是其他的方法实现了之后虽然可以实现功能,但是样式太丑,需要改css文件) 首先我的后

webform:分页组合查询

一个简单的分页组合查询页面 /// <summary> /// 查询方法 /// </summary> /// <param name="tsql">SQL语句</param> /// <param name="hh">哈希表</param> /// <returns></returns> public List<Goods> Select(string un

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

ADO.NET组合查询及其分页合并

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server&quo

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

ASP.NET 分页+组合查询 练习

分页和组合查询都是通过拼接SQL语句到数据库查询进行实现 到汽车表(car)中查询 ,汽车表选取了“编号 code”,“车名 name”,“日期 time”,“油耗 oil ”,“马力 powers”这几列 分页查询语句:每页只显示五条数据 查询前五条数据: select top 5 from car 点击下一页按钮:select top 5 from car where code not in(select top 5 code from car)//第二页的时候通过编号,查询前五条之后的数据

框架 day50 BOS项目 4 批量导入(ocupload插件,pinyin4J)/POI解析Excel/Combobox下拉框/分区组合条件分页查询(ajax)/分区数据导出(Excel)

知识点: 批量导入(ocupload插件,pinyin4J /POI解析Excel(apache POI) /区域分页查询 /Combobox下拉框 /分区组合条件分页查询(ajax) /分区数据导出(Excel下载) BOS项目笔记第4天 1.    区域批量导入功能 *Ajax不支持文件上传. *上传并且不刷新上传页面原理: Target到一个0,0,0的隐藏iframe里,造成一个没有刷新的假象 <form target="myIframe" action="ab