界面:
<%@ 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 思路二: