zoukankan      html  css  js  c++  java
  • 【2017-5-24】WebForm 条件查询

    实体类

        public int ids { get; set; }
        public string code { get; set; }
        public string name { get; set; }
        public decimal oil { get; set; }
        public decimal price { get; set; }

    封装类

    注意引用using System.Data.SqlClient;

    SqlConnection conn = null;
        SqlCommand cmd = null;
    
        public carData()
        {
            conn = new SqlConnection("server=.;database=One;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }
    
        //查询全部
        public List<car> SelectAll()
        {
            List<car> clist = new List<car>();
            cmd.CommandText = "select * from car";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    car c = new car();
                    c.ids = Convert.ToInt32(dr["ids"]);
                    c.code = dr["code"].ToString();
                    c.name = dr["name"].ToString();
                    c.oil = Convert.ToDecimal(dr["oil"]);
                    c.price = Convert.ToDecimal(dr["price"]);
                    clist.Add(c);
                }
            }
            conn.Close();
            return clist;
        }
    
    
        //分页查询
        public List<car> SelectAll(int count, int number)
        {
            List<car> clist = new List<car>();
            cmd.CommandText = "select top " + count + " * from car where ids not in(select top " + (count * (number - 1)) + " ids from car)";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    car c = new car();
                    c.ids = Convert.ToInt32(dr["ids"]);
                    c.code = dr["code"].ToString();
                    c.name = dr["name"].ToString();
                    c.oil = Convert.ToDecimal(dr["oil"]);
                    c.price = Convert.ToDecimal(dr["price"]);
                    clist.Add(c);
                }
            }
            conn.Close();
            return clist;
        }
    
    
        //查询数据条数
        public int SelectCount()
        {
            int a = 0;
            cmd.CommandText = "select count(*) from car";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            a = Convert.ToInt32(dr[0]);
            conn.Close();
            return a;
        }
    
    
        //条件查询
        public List<car> SelectAll(string tsql, Hashtable hs)
        {
            List<car> clist = new List<car>();
            cmd.CommandText = tsql;
            cmd.Parameters.Clear();
            foreach(string h in hs.Keys)
            {
            cmd.Parameters.AddWithValue(h,hs[h]);
            }
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    car c = new car();
                    c.ids = Convert.ToInt32(dr["ids"]);
                    c.code = dr["code"].ToString();
                    c.name = dr["name"].ToString();
                    c.oil = Convert.ToDecimal(dr["oil"]);
                    c.price = Convert.ToDecimal(dr["price"]);
                    clist.Add(c);
                }
            }
            conn.Close();
            return clist;
        }

    前端

    名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            油耗:<asp:DropDownList ID="DropDownList1" 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="TextBox2" runat="server"></asp:TextBox>
            价格:<asp:DropDownList ID="DropDownList2" runat="server">
                <asp:ListItem text="任意金额" Value="null"></asp:ListItem>
                <asp:ListItem text="小于30万" Value="price < 30"></asp:ListItem>
                <asp:ListItem text="大于30万小于40万" Value="price > 30 and price < 40"></asp:ListItem>
                <asp:ListItem text="大于40万小于50万" Value="price > 40 and price < 50"></asp:ListItem>
                <asp:ListItem text="大于50万" Value="price > 40"></asp:ListItem>
               </asp:DropDownList>
            &nbsp;&nbsp;&nbsp;&nbsp;
    
            <asp:Button ID="Button1" runat="server" Text="查询" />
    
    
    
    
    
    
            <br />
            <br />
            <br />
            <br />
            <br />
            <br />
            <table style=" 100%; background-color: #0094ff; text-align: center;">
                <tr>
                    <td>Ids</td>
                    <td>编号</td>
                    <td>名称</td>
                    <td>油耗</td>
                    <td>价格</td>
                </tr>
    
    
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <tr style="background-color: #fff;">
                            <td><%#Eval("ids") %></td>
                            <td><%#Eval("code") %></td>
                            <td><%#Eval("name") %></td>
                            <td><%#Eval("oil") %></td>
                            <td><%#Eval("price") %></td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>

    后台

    if (!IsPostBack)
            {
                Repeater1.DataSource = new carData().SelectAll();
                Repeater1.DataBind();
            }
            //查询按钮
            Button1.Click += Button1_Click;
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            //防止字符串注入攻击,将用户自己输得内容放到哈希表集合当中
            Hashtable hs = new Hashtable();
            int count = 0;
            string tsql = "select * from car ";
            if (TextBox1.Text.Trim().Length > 0)
            {
                tsql += "where name like @a ";
                hs.Add("@a", "%" + TextBox1.Text.Trim() + "%");
                count++;
            }
            if (TextBox2.Text.Trim().Length > 0)
            {
                if (count > 0)
                {
                    tsql += "and oil " + DropDownList1.SelectedValue + " @b ";
                }
                else
                {
                    tsql += "where oil " + DropDownList1.SelectedValue + " @b ";
                }
                hs.Add("@b", "TextBox2.Text.Trim()");
                count++;
            }
            if (DropDownList2.SelectedValue != "null")
            {
                if (count > 0)
                {
                    tsql += "and " + DropDownList2.SelectedValue;
                }
                else
                {
                    tsql += "where " + DropDownList2.SelectedValue;
                }
                count++;
            }
            //数据绑定
            Repeater1.DataSource = new carData().SelectAll(tsql,hs);
            Repeater1.DataBind();
        }
  • 相关阅读:
    MySQL优化查询语句Explain
    Spring Kafka(二)操作Topic以及Kafka Tool 2的使用
    集群作业管理OpenPBS与OpenPBS Interface Library
    OpenPBS 脚本样本 非常值得参考
    mpirun 与 PBS 问题
    MPIRUN 与 PBS 脚本
    什么是IA架构服务器
    集群的管理pbs
    软件包 javax.servlet 不存在
    An introduction to PORTABLE BATCH SYSTEM
  • 原文地址:https://www.cnblogs.com/hanqi0216/p/6902430.html
Copyright © 2011-2022 走看看