界面:

 <%@ 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">
<div> 名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
油耗:<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="True">
<asp:ListItem Value="=">等于</asp:ListItem>
<asp:ListItem Value="&gt;=">大于等于</asp:ListItem>
<asp:ListItem Value="&lt;=">小于等于</asp:ListItem>
</asp:DropDownList><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
价格:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>-<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="提交" /><br /><br />
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table style="background-color: blue; width: 100%; text-align:center">
<thead>
<tr style="color: white;">
<td>编号</td>
<td>名称</td>
<td>品牌</td>
<td>上市时间</td>
<td>油耗</td>
<td>动力</td>
<td>排量</td>
<td>价格</td>
<td>图片</td>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color: #808080">
<td><%#Eval("Code") %></td>
<td><%#Eval("Name") %></td>
<td><%#Eval("Brand") %></td>
<td><%#Eval("Time") %></td>
<td><%#Eval("Oil") %></td>
<td><%#Eval("Power") %></td>
<td><%#Eval("Exhaust") %></td>
<td><%#Eval("Price") %></td>
<td><%#Eval("Pic") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>

界面

后台:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Car 的摘要说明
/// </summary>
public class Car
{
public Car()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string Code { get; set; }
public string Name { get; set; }
public string Brand { get; set; }
public DateTime Time { get; set; }
public decimal Oil { get; set; }
public int Power { get; set; }
public decimal Exhaust { get; set; }
public decimal Price { get; set; }
public string Pic { get; set; }
}

封装实体类

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Collections; /// <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();
} public List<Car> Select()
{
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.Code = dr[].ToString();
c.Name = dr[].ToString();
c.Brand = dr[].ToString();
c.Time = Convert.ToDateTime(dr[]);
c.Oil = Convert.ToDecimal(dr[]);
c.Power = Convert.ToInt32(dr[]);
c.Exhaust = Convert.ToInt32(dr[]);
c.Price = Convert.ToDecimal(dr[]);
c.Pic = dr[].ToString(); clist.Add(c);
}
}
conn.Close();
return clist;
} public List<Car> Select(int count,int nowpage)
{
List<Car> clist = new List<Car>();
cmd.CommandText = "select top "+count+" *from Car where Code not in (select top "+((nowpage-)*count)+" Code from Car) "; conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Car c = new Car();
c.Code = dr[].ToString();
c.Name = dr[].ToString();
c.Brand = dr[].ToString();
c.Time = Convert.ToDateTime(dr[]);
c.Oil = Convert.ToDecimal(dr[]);
c.Power = Convert.ToInt32(dr[]);
c.Exhaust = Convert.ToInt32(dr[]);
c.Price = Convert.ToDecimal(dr[]);
c.Pic = dr[].ToString(); clist.Add(c);
}
}
conn.Close();
return clist;
} public List<Car> Select(string sql,Hashtable hat)
{
List<Car> clist = new List<Car>();
cmd.CommandText = sql;
cmd.Parameters.Clear(); foreach(string s in hat.Keys)
{
cmd.Parameters.AddWithValue(s,hat[s]);
} conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Car c = new Car();
c.Code = dr[].ToString();
c.Name = dr[].ToString();
c.Brand = dr[].ToString();
c.Time = Convert.ToDateTime(dr[]);
c.Oil = Convert.ToDecimal(dr[]);
c.Power = Convert.ToInt32(dr[]);
c.Exhaust = Convert.ToInt32(dr[]);
c.Price = Convert.ToDecimal(dr[]);
c.Pic = dr[].ToString(); clist.Add(c);
}
}
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 _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 has=new Hashtable ();
string tsql="select *from Car";
//判断文本框中是否有内容需要查询
if (TextBox1.Text.Trim().Length > )
{//如果有内容,那么就拼接到Tsql语句中去
tsql += " where name like @name";
has.Add("@name","%"+TextBox1.Text.Trim().ToUpper()+"%");
}
else
{
tsql += " where 1=1";
}
if (TextBox2.Text.Trim().Length > )
{
tsql += " and oil "+DropDownList1.SelectedValue+"@oil";
has.Add("@oil", TextBox2.Text.Trim());
}
else
{
tsql += " and 1=1";
}
if (TextBox3.Text.Trim().Length > )
{
tsql += " and price>=@price1";
has.Add("@price1", TextBox3.Text.Trim());
}
else
{
tsql += " and 1=1";
}
if (TextBox4.Text.Trim().Length > )
{
tsql += " and price<=@price2";
has.Add("@price2", TextBox4.Text.Trim());
}
//将拼接好的Tsql语句执行后进行数据绑定
Repeater1.DataSource = new CarData().Select(tsql,has);
Repeater1.DataBind();
}
}

Default.aspx.cs 思路一:

 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 has = new Hashtable();
string tsql = "select *from Car";
int count = ;
//判断文本框中是否有内容需要查询
if (TextBox1.Text.Trim().Length > )
{//如果有内容,那么就拼接到Tsql语句中去
tsql += " where name like @name";
has.Add("@name", "%" + TextBox1.Text.Trim().ToUpper() + "%");
count++;
} if (TextBox2.Text.Trim().Length > )
{
if (count > )
tsql += " and oil " + DropDownList1.SelectedValue + "@oil";
else
tsql += " where oil " + DropDownList1.SelectedValue + "@oil"; has.Add("@oil", TextBox2.Text.Trim());
count++;
} if (TextBox3.Text.Trim().Length > )
{
if (count > )
tsql += " and price>=@price1";
else
tsql += " where price>=@price1";
has.Add("@price1", TextBox3.Text.Trim());
count++;
} if (TextBox4.Text.Trim().Length > )
{
if (count > )
tsql += " and price<=@price2";
else
tsql += " where price<=@price2";
has.Add("@price2", TextBox4.Text.Trim());
}
//将拼接好的Tsql语句执行后进行数据绑定
Repeater1.DataSource = new CarData().Select(tsql, has);
Repeater1.DataBind();
}
}

Default.aspx.cs 思路二:

webform 组合查询的更多相关文章

  1. webform组合查询和分页

    1.组合查询(1)数据访问类 //参数1:SQL语句 参数2:哈希表public List<Users> chas(string s,Hashtable has) { List<Us ...

  2. WebForm组合查询

    封转类 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <sum ...

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

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

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

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

  5. Webform(分页与组合查询配合使用)

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

  6. webform:分页组合查询

    一个简单的分页组合查询页面 /// <summary> /// 查询方法 /// </summary> /// <param name="tsql"& ...

  7. Webform(分页、组合查询)

    一.分页 1.写查询方法: public List<Student> Select(int PageCount, int PageNumber) {//PageCount为每页显示条数,P ...

  8. WebForm之Linq组合查询

    组合查询 protected void Button1_Click(object sender, EventArgs e) { //默认查询所有,返回的是Table类型,转换成IQueryAble类型 ...

  9. WebForm 分页与组合查询

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

随机推荐

  1. Windows Server 2008 双网卡同时上内外网 不能正常使用

    Windows server 2008 32位下,双网卡同时上内外网,并提供VPN服务,遇见的奇怪问题 1.服务器配置 2.网络配置 以太网适配器 内部连接: 连接特定的 DNS 后缀 . . . . ...

  2. myabatis oracle 调用存储过程返回list结果集

    Mapper.xml 配置 <resultMap type="emp" id="empMap"> <id property="emp ...

  3. html5 Application Cache 机制以及使用

    那什么是Application Cache呢? 顾名思义,AppCache就是对app内存缓存的方案,具体表现为当请求某个文件时不是从网络获取该文件,而是从本地内存中获取. Application C ...

  4. netstat相关

    1. netstat基本参数: 参数 说明 输出 -a 列出所有连接. 列出 tcp, udp 和 unix 协议下所有套接字的所有连接.然而这些信息还不够详细,管理员往往需要查看某个协议或端口的具体 ...

  5. 模拟搭建Web项目的真实运行环境(五)

    一.开启IIS功能 刚安装完的server2008是没有默认开启IIS功能,在这里简单介绍一下如何开启IIS. 步骤: 1. 打开控制面板,选中[程序] 2. 在[程序和功能]下面,选择[打开或关闭w ...

  6. C#设计模式之建造者模式

    建造者模式可以将部件本身和它们的组装过程分开,关注如何一步步创建一个包含多个组成部分的复杂对象,用户只需要指定复杂对象的类型即可得到该对象,而无须知道其内部的具体构造细节. 建造者模式:将一个复杂对象 ...

  7. php五种常见的设计模式(转载)

    很多人都想着写博客来记录编程生活中的点滴,我也不例外,但想了好长时间不知道写什么........万事开头难,先转载一篇吧..... 设计模式 一书将设计模式引入软件社区,该书的作者是 Erich Ga ...

  8. Enterprise Achitect使用与类的关系的简单介绍

    本文作为Enterprise Achitect初步使用,另外也是类图基本介绍,是设计模式的基础.  类的关系有泛化(Generalization).实现(Realization).依赖(Depende ...

  9. 【转载】关于treeview的多层显示的科学用法!

    http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-b ...

  10. word使用笔记(1)

    开始字母的格式也是宋体,只要全选后设置字体为Times New Roman即可,会自动跳过中文 新安的Mathtype字体有点奇怪,在样式里设置了一下,果然自定义为宋体了,改回Times New Ro ...