webform 组合查询
界面:
<%@ 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=">=">大于等于</asp:ListItem>
<asp:ListItem Value="<=">小于等于</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 组合查询的更多相关文章
- webform组合查询和分页
1.组合查询(1)数据访问类 //参数1:SQL语句 参数2:哈希表public List<Users> chas(string s,Hashtable has) { List<Us ...
- WebForm组合查询
封转类 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <sum ...
- webform 分页、组合查询综合使用
界面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx ...
- Webform(Linq高级查、分页、组合查询)
一.linq高级查 1.模糊查(包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r => ...
- Webform(分页与组合查询配合使用)
1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name) { List<Subject> ...
- webform:分页组合查询
一个简单的分页组合查询页面 /// <summary> /// 查询方法 /// </summary> /// <param name="tsql"& ...
- Webform(分页、组合查询)
一.分页 1.写查询方法: public List<Student> Select(int PageCount, int PageNumber) {//PageCount为每页显示条数,P ...
- WebForm之Linq组合查询
组合查询 protected void Button1_Click(object sender, EventArgs e) { //默认查询所有,返回的是Table类型,转换成IQueryAble类型 ...
- WebForm 分页与组合查询
1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name) { List<Subject> ...
随机推荐
- iOS 手势识别器(UIGestureRecognizer)
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势. UIGestureRecognizer的子类有: UITapGestureRecogni ...
- Sublime Text:Windows下配置C 编译环境和GDB调试环境
写此文解决两个问题: 1.在Sublime Text中实现编译运行含有外部输入的C程序(如含有scanf的程序); 2.在程序运行完毕后不退出cmd,能继续用gdb调试程序. 一.MinGW 下载地址 ...
- UIRefreshControl
在iOS6中UITableViewController 已经集成了UIRefreshControl 控件.UIRefreshControl目前只能用于UITableViewController
- 基于SSM的租赁管理系统0.2_20161225_开发环境
项目环境搭建 1. 开发环境 Sybase PowerDesigner 15.1.0 + MySQL 5.7.15 + Navicat 11.0.9 + eclipse EE Mars 2.0 + F ...
- MVC -- 后台RedirectToAction传递实体类与字符串
1.MVC -- 后台RedirectToAction传递实体类 RedirectToAction(控制器,控制器方法,实体类) 2.MVC -- 后台RedirectToAction传递字符串 Re ...
- Appium_Python_Api文档
1.contextscontexts(self): Returns the contexts within the current session. 返回当前会话中的上下文,使用后可以识别H5页面的控 ...
- OE学习笔记流水
Terrain.cpp中的getWorldCoordsUnderMouse函数,进行标记.
- 在nginx中配置ip直接访问的默认站点
一台机子部署多个网站,我们直接访问ip (外网内网都一样)提示无法访问或404. 因为有多个网站,我们想指定某个网站为IP访问默认的网站,如何操作呢? 解:在Listen ip:port; 这个指令行 ...
- linux 命令 ---- 同步当前服务器时间
原因:昨天临走前,虚拟机没有关机,是挂起状态,然后今天来的时候,发现数据库表中存(更新)的时间,不是系统时间, 解决:先运行起我们的虚拟机, (对于asterisk) 1.先查看当前服务器(linux ...
- SQL函数说明大全
一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...