首页 > 代码库 > WebForm组合查询
WebForm组合查询
封转类
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Car 的摘要说明 /// </summary> public class Car { public int ids{get;set;} public string code{get;set;} public string name{get;set;} public string brand{get;set;} public decimal oil{get;set;} public int powers{get;set;} public decimal price{get;set;} }
建立方法
using System; using System.Collections; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; /// <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(); } /// <summary> /// 查询所有数据信息 /// </summary> /// <returns>泛型集合</returns> public List<Car> Select() { List<Car> list = 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.ids = Convert.ToInt32(dr[0]); c.code = dr[1].ToString(); c.name = dr[2].ToString(); c.brand = dr[3].ToString(); c.oil = Convert.ToDecimal(dr["oil"]); c.powers = Convert.ToInt32(dr["powers"]); c.price = Convert.ToDecimal(dr["price"]); list.Add(c); } } conn.Close(); return list; } public List<Car> Select(int PageCount, int PageNumber) { List<Car> list = new List<Car>(); cmd.CommandText = "select top " + PageCount + " *from car where ids not in(select top "+(PageCount * (PageNumber - 1))+" ids from car)"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Car c = new Car(); c.ids = Convert.ToInt32(dr[0]); c.code = dr[1].ToString(); c.name = dr[2].ToString(); c.brand = dr[3].ToString(); c.oil = Convert.ToDecimal(dr["oil"]); c.powers = Convert.ToInt32(dr["powers"]); c.price = Convert.ToDecimal(dr["price"]); list.Add(c); } } conn.Close(); return list; } /// <summary> /// 查询符合SQl条件的所有信息 /// </summary> /// <param name="Tsql">查询条件</param> /// <param name="hhh">防止字符串注入攻击,把字符串加入哈希表集合中</param> /// <returns></returns> public List<Car> Select(string Tsql,Hashtable hhh) { List<Car> list = new List<Car>(); cmd.CommandText = Tsql; cmd.Parameters.Clear(); foreach(string k in hhh.Keys) { cmd.Parameters.Add(k, hhh[k]); } conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Car c = new Car(); c.ids = Convert.ToInt32(dr[0]); c.code = dr[1].ToString(); c.name = dr[2].ToString(); c.brand = dr[3].ToString(); c.oil = Convert.ToDecimal(dr["oil"]); c.powers = Convert.ToInt32(dr["powers"]); c.price = Convert.ToDecimal(dr["price"]); list.Add(c); } } conn.Close(); return list; } }
页面设置
<%@ 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"> name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> oil:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> price:<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>>=</asp:ListItem> <asp:ListItem><=</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="查询" /><br /><br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /><br /> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table style="width: 100%; background-color: navy; text-align: center;"> <tr style="color: white;"> <td>ids</td> <td>code</td> <td>name</td> <td>brand</td> <td>oil</td> <td>powers</td> <td>price</td> </tr> </HeaderTemplate> <ItemTemplate> <tr style="background-color: white;"> <td><%#Eval("ids") %></td> <td><%#Eval("code") %></td> <td><%#Eval("name") %></td> <td><%#Eval("brand") %></td> <td><%#Eval("oil") %></td> <td><%#Eval("powers") %></td> <td><%#Eval("price") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
方法事件(代码)
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 hs = new Hashtable(); int count = 0;//中间变量,查看前面是否有查询条件 string Tsql = "select *from car"; if (!String.IsNullOrEmpty(TextBox1.Text.Trim())) { Tsql += " where name like @a"; hs.Add("@a", "%" + TextBox1.Text.Trim() + "%"); count++;//如果该条件写入内容,就加1,表示这里有查询条件 } if (!String.IsNullOrEmpty(TextBox2.Text.Trim())) { if (count == 0)//如果前面没有查询条件,就用where { Tsql += " where oil = @b"; } else//如果前面有查询条件,就用and { Tsql += " and oil = @b"; } hs.Add("@b", TextBox2.Text.Trim()); count++; } if (!String.IsNullOrEmpty(TextBox3.Text.Trim())) { if (count == 0) { Tsql += " where price " + DropDownList1.SelectedValue + " @c"; } else { Tsql += " and price " + DropDownList1.SelectedValue + " @c"; } hs.Add("@c", TextBox3.Text.Trim()); count++; } Label1.Text = Tsql; Repeater1.DataSource = new CarData().Select(Tsql, hs);//数据源重新指向 Repeater1.DataBind();//数据绑定 } }
总之:组合查询还是拼接SQl查询语句。
完!!
WebForm组合查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。