界面:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2
 3 <!DOCTYPE html>
 4
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13
14         名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
15         油耗:<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="True">
16             <asp:ListItem Value="=">等于</asp:ListItem>
17             <asp:ListItem Value="&gt;=">大于等于</asp:ListItem>
18             <asp:ListItem Value="&lt;=">小于等于</asp:ListItem>
19         </asp:DropDownList><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
20         价格:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>-<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
21         <asp:Button ID="Button1" runat="server" Text="提交" /><br /><br />
22         <asp:Repeater ID="Repeater1" runat="server">
23                 <HeaderTemplate>
24                     <table style="background-color: blue; width: 100%; text-align:center">
25                         <thead>
26                             <tr style="color: white;">
27                                 <td>编号</td>
28                                 <td>名称</td>
29                                 <td>品牌</td>
30                                 <td>上市时间</td>
31                                 <td>油耗</td>
32                                 <td>动力</td>
33                                 <td>排量</td>
34                                 <td>价格</td>
35                                 <td>图片</td>
36                             </tr>
37                         </thead>
38                         <tbody>
39                 </HeaderTemplate>
40                 <ItemTemplate>
41                     <tr style="background-color: #808080">
42                         <td><%#Eval("Code") %></td>
43                         <td><%#Eval("Name") %></td>
44                         <td><%#Eval("Brand") %></td>
45                         <td><%#Eval("Time") %></td>
46                         <td><%#Eval("Oil") %></td>
47                         <td><%#Eval("Power") %></td>
48                         <td><%#Eval("Exhaust") %></td>
49                         <td><%#Eval("Price") %></td>
50                         <td><%#Eval("Pic") %></td>
51                     </tr>
52                 </ItemTemplate>
53                 <FooterTemplate>
54                     </tbody>
55             </table>
56                 </FooterTemplate>
57             </asp:Repeater>
58     </div>
59     </form>
60 </body>
61 </html>

界面

后台:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5
 6 /// <summary>
 7 /// Car 的摘要说明
 8 /// </summary>
 9 public class Car
10 {
11     public Car()
12     {
13         //
14         // TODO: 在此处添加构造函数逻辑
15         //
16     }
17     public string Code { get; set; }
18     public string Name { get; set; }
19     public string Brand { get; set; }
20     public DateTime Time { get; set; }
21     public decimal Oil { get; set; }
22     public int Power { get; set; }
23     public decimal Exhaust { get; set; }
24     public decimal Price { get; set; }
25     public string Pic { get; set; }
26 }

封装实体类

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Data.SqlClient;
  6 using System.Collections;
  7
  8 /// <summary>
  9 /// CarData 的摘要说明
 10 /// </summary>
 11 public class CarData
 12 {
 13     SqlConnection conn = null;
 14     SqlCommand cmd = null;
 15     public CarData()
 16     {
 17         conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
 18         cmd = conn.CreateCommand();
 19     }
 20
 21     public List<Car> Select()
 22     {
 23         List<Car> clist = new List<Car>();
 24         cmd.CommandText = "select *from Car";
 25
 26         conn.Open();
 27         SqlDataReader dr = cmd.ExecuteReader();
 28         if (dr.HasRows)
 29         {
 30             while (dr.Read())
 31             {
 32                 Car c = new Car();
 33                 c.Code = dr[0].ToString();
 34                 c.Name = dr[1].ToString();
 35                 c.Brand = dr[2].ToString();
 36                 c.Time = Convert.ToDateTime(dr[3]);
 37                 c.Oil = Convert.ToDecimal(dr[4]);
 38                 c.Power = Convert.ToInt32(dr[5]);
 39                 c.Exhaust = Convert.ToInt32(dr[6]);
 40                 c.Price = Convert.ToDecimal(dr[7]);
 41                 c.Pic = dr[8].ToString();
 42
 43                 clist.Add(c);
 44             }
 45         }
 46         conn.Close();
 47         return clist;
 48     }
 49
 50
 51     public List<Car> Select(int count,int nowpage)
 52     {
 53         List<Car> clist = new List<Car>();
 54         cmd.CommandText = "select top "+count+" *from Car where Code not in (select top "+((nowpage-1)*count)+" Code from Car) ";
 55
 56         conn.Open();
 57         SqlDataReader dr = cmd.ExecuteReader();
 58         if (dr.HasRows)
 59         {
 60             while (dr.Read())
 61             {
 62                 Car c = new Car();
 63                 c.Code = dr[0].ToString();
 64                 c.Name = dr[1].ToString();
 65                 c.Brand = dr[2].ToString();
 66                 c.Time = Convert.ToDateTime(dr[3]);
 67                 c.Oil = Convert.ToDecimal(dr[4]);
 68                 c.Power = Convert.ToInt32(dr[5]);
 69                 c.Exhaust = Convert.ToInt32(dr[6]);
 70                 c.Price = Convert.ToDecimal(dr[7]);
 71                 c.Pic = dr[8].ToString();
 72
 73                 clist.Add(c);
 74             }
 75         }
 76         conn.Close();
 77         return clist;
 78     }
 79
 80     public List<Car> Select(string sql,Hashtable hat)
 81     {
 82         List<Car> clist = new List<Car>();
 83         cmd.CommandText = sql;
 84         cmd.Parameters.Clear();
 85
 86         foreach(string s in hat.Keys)
 87         {
 88             cmd.Parameters.AddWithValue(s,hat[s]);
 89         }
 90
 91         conn.Open();
 92         SqlDataReader dr = cmd.ExecuteReader();
 93         if (dr.HasRows)
 94         {
 95             while (dr.Read())
 96             {
 97                 Car c = new Car();
 98                 c.Code = dr[0].ToString();
 99                 c.Name = dr[1].ToString();
100                 c.Brand = dr[2].ToString();
101                 c.Time = Convert.ToDateTime(dr[3]);
102                 c.Oil = Convert.ToDecimal(dr[4]);
103                 c.Power = Convert.ToInt32(dr[5]);
104                 c.Exhaust = Convert.ToInt32(dr[6]);
105                 c.Price = Convert.ToDecimal(dr[7]);
106                 c.Pic = dr[8].ToString();
107
108                 clist.Add(c);
109             }
110         }
111         conn.Close();
112         return clist;
113     }
114 }

数据访问类

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8
 9 public partial class _Default : System.Web.UI.Page
10 {
11     protected void Page_Load(object sender, EventArgs e)
12     {
13         if (!IsPostBack)
14         {
15             Repeater1.DataSource = new CarData().Select();
16             Repeater1.DataBind();
17         }
18         Button1.Click += Button1_Click;
19     }
20
21     void Button1_Click(object sender, EventArgs e)
22     {
23         Hashtable has=new Hashtable ();
24         string tsql="select *from Car";
25         //判断文本框中是否有内容需要查询
26         if (TextBox1.Text.Trim().Length > 0)
27         {//如果有内容,那么就拼接到Tsql语句中去
28             tsql += " where name like @name";
29             has.Add("@name","%"+TextBox1.Text.Trim().ToUpper()+"%");
30         }
31         else
32         {
33             tsql += " where 1=1";
34         }
35         if (TextBox2.Text.Trim().Length > 0)
36         {
37             tsql += " and oil "+DropDownList1.SelectedValue+"@oil";
38             has.Add("@oil", TextBox2.Text.Trim());
39         }
40         else
41         {
42             tsql += " and 1=1";
43         }
44         if (TextBox3.Text.Trim().Length > 0)
45         {
46             tsql += " and price>=@price1";
47             has.Add("@price1", TextBox3.Text.Trim());
48         }
49         else
50         {
51             tsql += " and 1=1";
52         }
53         if (TextBox4.Text.Trim().Length > 0)
54         {
55             tsql += " and price<=@price2";
56             has.Add("@price2", TextBox4.Text.Trim());
57         }
58         //将拼接好的Tsql语句执行后进行数据绑定
59         Repeater1.DataSource = new CarData().Select(tsql,has);
60         Repeater1.DataBind();
61     }
62 }

Default.aspx.cs 思路一:

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8
 9 public partial class _Default : System.Web.UI.Page
10 {
11     protected void Page_Load(object sender, EventArgs e)
12     {
13         if (!IsPostBack)
14         {
15             Repeater1.DataSource = new CarData().Select();
16             Repeater1.DataBind();
17         }
18         Button1.Click += Button1_Click;
19     }
20
21     void Button1_Click(object sender, EventArgs e)
22     {
23         Hashtable has = new Hashtable();
24         string tsql = "select *from Car";
25         int count = 0;
26         //判断文本框中是否有内容需要查询
27         if (TextBox1.Text.Trim().Length > 0)
28         {//如果有内容,那么就拼接到Tsql语句中去
29             tsql += " where name like @name";
30             has.Add("@name", "%" + TextBox1.Text.Trim().ToUpper() + "%");
31             count++;
32         }
33
34         if (TextBox2.Text.Trim().Length > 0)
35         {
36             if (count > 0)
37                 tsql += " and oil " + DropDownList1.SelectedValue + "@oil";
38             else
39                 tsql += " where oil " + DropDownList1.SelectedValue + "@oil";
40
41             has.Add("@oil", TextBox2.Text.Trim());
42             count++;
43         }
44
45         if (TextBox3.Text.Trim().Length > 0)
46         {
47             if (count > 0)
48                 tsql += " and price>=@price1";
49             else
50                 tsql += " where price>=@price1";
51             has.Add("@price1", TextBox3.Text.Trim());
52             count++;
53         }
54
55         if (TextBox4.Text.Trim().Length > 0)
56         {
57             if (count > 0)
58                 tsql += " and price<=@price2";
59             else
60                 tsql += " where price<=@price2";
61             has.Add("@price2", TextBox4.Text.Trim());
62         }
63         //将拼接好的Tsql语句执行后进行数据绑定
64         Repeater1.DataSource = new CarData().Select(tsql, has);
65         Repeater1.DataBind();
66     }
67 }

Default.aspx.cs 思路二:

转载于:https://www.cnblogs.com/maxin991025-/p/6251523.html

webform 组合查询相关推荐

  1. webform 分页、组合查询综合使用

    界面: 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...

  2. Webform(Linq高级查、分页、组合查询)

    一.linq高级查 1.模糊查(包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r => ...

  3. WebForm 分页与组合查询

    1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name){List<Subject> li ...

  4. MySQL 学习笔记(4)— 组合查询、子查询、插入数据、更新/删除表数据、增加/删除表中的列以及重命名表

    1. 组合查询 1.表的加减法 表的加法,即求 product 和 product2 的并集,UNION 运算会除去重复的记录 SELECT product_id, product_name FROM ...

  5. 多条件组合查询+分页

    比较麻烦,把他记录下来. 前台UI: 实现的类代码: 程序代码 /// <summary>         /// 多条件组合查询         /// </summary> ...

  6. Mybatis实现多对多关联组合查询

    个人网站:http://xiaocaoshare.com/ 1.需求 用户信息表.标签表.用户标签关联信息表 在做用户列表查询的时候,需要查询出该用户对应的用户标签 <resultMap id= ...

  7. 使用python对学生表的查询_多表组合查询——Python操作Mysql数据库

    前面我们介绍了单张表的查询,包括模糊查询.分组.排序.各种筛选条件等等操作,在实际应用中,查询的数据往往不止局限在一张表里,通常需要多张表在一起进行组合查询,今天我们将会对Mysql当中的多张有关联的 ...

  8. 13.组合查询--SQL

    利用UNION操作符将多条SELECT语句组合成一个结果集. 主要有两种情况需要使用组合查询: 在一个查询中从不同的表返回结构数据: 对一个表执行多个查询,按一个查询返回数据. UNION规则 UNI ...

  9. SQL学习之组合查询(UNION)

    1.大多数的SQL查询只包含从一个或多个表中返回数据的单条SELECT语句,但是,SQL也允许执行多个查询(多条SELECT语句),并将结果作为一个查询结果集返回.这些组合查询通常称为并或复合查询. ...

  10. 组合查询——怎样使用窗口的继承达到事半功倍?

    一个优秀的程序猿就是要尽可能降低自己的代码量.我们日常的工作或者学习中都有可能遇到多个窗口结构类似,或者大同小异的情况,这时候假设在每一个窗口都写一遍代码,或者纯粹的复制粘贴就太水了.想要偷懒的时候正 ...

最新文章

  1. NET Core微服务之路:再谈分布式系统中一致性问题分析
  2. .NET Core log4net 使用
  3. Single Image Haze Removal(图像去雾)-CVPR’09 Best Paper
  4. 前端工程师,会是一群高情商的程序猿
  5. 用C语言实现猜数游戏
  6. 网易互娱2020游戏研发实习生笔经面经
  7. 如何在Google地图上找到经度和纬度
  8. python银行账户资金交易管理_Python实现银行账户资金交易管理系统
  9. 爬取巨潮资讯网中与“贵州茅台”相关的公告的标题和网址。
  10. IDEA上的GIT PULL的各选项含义
  11. CALPHAD方法中“外推”的理解
  12. 一文学明白数据库系统--数据库系统期末复习--从入门到入考场--考前抄ppt系列
  13. 1. R语言介绍、Rstudio的基本使用、帮助命令、内置数据集
  14. 手机密码大全及国产贴牌与OEM型号对照表
  15. 二叉树的叶子结点按从左到右的顺序连成一个单链表
  16. 报错FileSystemException: /datas/nodes/0/indices/gtTXk-hnTgKhAcm-8n60Jw/1/index/.es_temp_file:结构需要清理
  17. 个人项目——基于STM32的智能物联网寝室
  18. unity人物基础动画应用。行走、待机之类的简单动画切换。
  19. python天气查询运行过程_菜鸟玩Python|制作天气查询软件
  20. 软件测试周刊(第19期):以能力、流程、指标和工具为轴心打造一流的质量保证部门

热门文章

  1. php数据库数据分割,使用PHP将分隔的值文件导入数据库时??,...
  2. Vmware安装vmware-tools后,仍无法上网
  3. jeecgboot 查询_Jeecg-Boot 技术文档
  4. windows server 系统SERVER服务消失无法共享
  5. 皕杰报表使用技巧:竖排文字如何输入
  6. 请问两个div之间的上下距离怎么设置
  7. 关于sql 拼接字符串的问题
  8. 你想面试运维看一下你合格了吗?
  9. 运维自动化之ansible playbook安装mysql
  10. Stream上传插件(Java接口实现)