首页 > Web开发 > 详细

WebForm组合查询

时间:2016-10-21 23:43:16      阅读:357      评论:0      收藏:0      [点我收藏+]

封转类

技术分享
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;}
}
View Code

建立方法

技术分享
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;
    }


}
View Code

页面设置

技术分享
<%@ 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>&nbsp;
        oil:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>&nbsp;
        price:<asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>&gt;=</asp:ListItem>
            <asp:ListItem>&lt;=</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>&nbsp;
        <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>
View Code

方法事件(代码)

技术分享
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();//数据绑定




    }
}
View Code

总之:组合查询还是拼接SQl查询语句。

 

完!!

 

WebForm组合查询

原文:http://www.cnblogs.com/wwz-wwz/p/5986179.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!