zoukankan      html  css  js  c++  java
  • WebForm组合查询

    封转类

    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 code{get;set;}
        public string name{get;set;}
        public string brand{get;set;}
        public decimal oil{get;set;}
        public int powers{get;set;}
        public decimal price{get;set;}
    }
    View Code

    建立方法

    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=mydb;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }
        /// <summary>
        /// 查询所有数据信息
        /// </summary>
        /// <returns>泛型集合</returns>
        public List<Car> Select()
        {
            List<Car> list = 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[0]);
                    c.code = dr[1].ToString();
                    c.name = dr[2].ToString();
                    c.brand = dr[3].ToString();
                    c.oil = Convert.ToDecimal(dr["oil"]);
                    c.powers = Convert.ToInt32(dr["powers"]);
                    c.price = Convert.ToDecimal(dr["price"]);
                    list.Add(c);
                }
            }
            conn.Close();
            return list;
        }
    
        public List<Car> Select(int PageCount, int PageNumber)
        {
            List<Car> list = new List<Car>();
            cmd.CommandText = "select top " + PageCount + " *from car where ids not in(select top "+(PageCount * (PageNumber - 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[0]);
                    c.code = dr[1].ToString();
                    c.name = dr[2].ToString();
                    c.brand = dr[3].ToString();
                    c.oil = Convert.ToDecimal(dr["oil"]);
                    c.powers = Convert.ToInt32(dr["powers"]);
                    c.price = Convert.ToDecimal(dr["price"]);
                    list.Add(c);
                }
            }
            conn.Close();
            return list;
        }
    
        /// <summary>
        /// 查询符合SQl条件的所有信息
        /// </summary>
        /// <param name="Tsql">查询条件</param>
        /// <param name="hhh">防止字符串注入攻击,把字符串加入哈希表集合中</param>
        /// <returns></returns>
        public List<Car> Select(string Tsql,Hashtable hhh)
        {
            List<Car> list = new List<Car>();
            cmd.CommandText = Tsql;
            cmd.Parameters.Clear();
            foreach(string k in hhh.Keys)
            {
                cmd.Parameters.Add(k, hhh[k]);
            }
    
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Car c = new Car();
                    c.ids = Convert.ToInt32(dr[0]);
                    c.code = dr[1].ToString();
                    c.name = dr[2].ToString();
                    c.brand = dr[3].ToString();
                    c.oil = Convert.ToDecimal(dr["oil"]);
                    c.powers = Convert.ToInt32(dr["powers"]);
                    c.price = Convert.ToDecimal(dr["price"]);
                    list.Add(c);
                }
            }
            conn.Close();
            return list;
        }
    
    
    }
    View Code

    页面设置

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!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">
            name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;
            oil:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>&nbsp;
            price:<asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>&gt;=</asp:ListItem>
                <asp:ListItem>&lt;=</asp:ListItem>
            </asp:DropDownList>
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>&nbsp;
            <asp:Button ID="Button1" runat="server" Text="查询" /><br /><br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /><br />
    
            <asp:Repeater ID="Repeater1" runat="server">
                <HeaderTemplate>
                    <table style=" 100%; background-color: navy; text-align: center;">
                        <tr style="color: white;">
                            <td>ids</td>
                            <td>code</td>
                            <td>name</td>
                            <td>brand</td>
                            <td>oil</td>
                            <td>powers</td>
                            <td>price</td>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr style="background-color: white;">
                        <td><%#Eval("ids") %></td>
                        <td><%#Eval("code") %></td>
                        <td><%#Eval("name") %></td>
                        <td><%#Eval("brand") %></td>
                        <td><%#Eval("oil") %></td>
                        <td><%#Eval("powers") %></td>
                        <td><%#Eval("price") %></td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </form>
    </body>
    </html>
    View Code

    方法事件(代码)

    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 _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Repeater1.DataSource = new CarData().Select();
                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 (!String.IsNullOrEmpty(TextBox1.Text.Trim()))
            {
                Tsql += " where name like @a";
                hs.Add("@a", "%" + TextBox1.Text.Trim() + "%");
                count++;//如果该条件写入内容,就加1,表示这里有查询条件
            }
    
            if (!String.IsNullOrEmpty(TextBox2.Text.Trim()))
            {
                if (count == 0)//如果前面没有查询条件,就用where
                {
                    Tsql += " where oil = @b";
                }
                else//如果前面有查询条件,就用and
                {
                    Tsql += " and oil = @b";
                }
                hs.Add("@b", TextBox2.Text.Trim());
                count++;
            }
    
            if (!String.IsNullOrEmpty(TextBox3.Text.Trim()))
            {
                if (count == 0)
                {
                    Tsql += " where price " + DropDownList1.SelectedValue + " @c";
                }
                else
                {
                    Tsql += " and price " + DropDownList1.SelectedValue + " @c";
                }
                hs.Add("@c", TextBox3.Text.Trim());
                count++;
            }
    
    
            Label1.Text = Tsql;
    
    
            Repeater1.DataSource = new CarData().Select(Tsql, hs);//数据源重新指向
            Repeater1.DataBind();//数据绑定
    
    
    
    
        }
    }
    View Code

    总之:组合查询还是拼接SQl查询语句。

    完!!

  • 相关阅读:
    免费的视频、音频转文本
    Errors are values
    Codebase Refactoring (with help from Go)
    Golang中的坑二
    Cleaner, more elegant, and wrong(msdn blog)
    Cleaner, more elegant, and wrong(翻译)
    Cleaner, more elegant, and harder to recognize(翻译)
    vue控制父子组件渲染顺序
    computed 和 watch 组合使用,监听数据全局数据状态
    webstorm破解方法
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/5986179.html
Copyright © 2011-2022 走看看