博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webform 组合查询
阅读量:6821 次
发布时间:2019-06-26

本文共 8704 字,大约阅读时间需要 29 分钟。

界面:

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 2  3  4  5  6  7 
8 9 10 11
12
13 14 名称:
15 油耗:
16
等于
17
大于等于
18
小于等于
19
20 价格:
-
21
22
23
24
41
54
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 40
42
43
44
45
46
47
48
49
50
51
52 53
55
编号 名称 品牌 上市时间 油耗 动力 排量 价格 图片
<%#Eval("Code") %> <%#Eval("Name") %> <%#Eval("Brand") %> <%#Eval("Time") %> <%#Eval("Oil") %> <%#Eval("Power") %> <%#Eval("Exhaust") %> <%#Eval("Price") %> <%#Eval("Pic") %>
56 57
58
59
60 61
界面

后台:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5  6 ///  7 /// Car 的摘要说明 8 ///  9 public class Car10 {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 ///   9 /// CarData 的摘要说明 10 ///  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
Select() 22 { 23 List
clist = new List
(); 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
Select(int count,int nowpage) 52 { 53 List
clist = new List
(); 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
Select(string sql,Hashtable hat) 81 { 82 List
clist = new List
(); 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.Page10 {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 else32 {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 else41 {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 else50 {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.Page10 {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 else39 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 else50 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 else60 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

你可能感兴趣的文章
计划任务
查看>>
获取无序数组中第n大的数及快速排序算法使用
查看>>
我的友情链接
查看>>
MongoDB复制集原理
查看>>
Java开发(2) - Tomcat配置JNDI数据源
查看>>
Highcharts error #12 问题解决办法
查看>>
HA配置方案
查看>>
手机号码 正则
查看>>
如何解酷派CPB包
查看>>
Linux 安装JDK,配置JAVA环境变量
查看>>
jenkins插件之小白的笔记
查看>>
html meta中的viewport指令
查看>>
windows 2008的安装
查看>>
Unity3D研究院之手游开发中所有特殊的文件夹(assetbundle与Application.persistentDataPath)...
查看>>
[DeviceOne开发]-手势动画示例分享
查看>>
《Activiti实战》读书笔记——5.1.4
查看>>
Linux文件管理类命令
查看>>
Kuerbernetes 1.11 二进制安装
查看>>
SpringMVC异步处理之@Async(附源代码 - 单元测试通过)
查看>>
undefined reference to 'pthread_setspecific '
查看>>