asp.net+SQL2005实现字符串的模糊查询

通常写一个简单的模糊查询的SQL语句格式可以如下例: 说明:1、WHERE 关键词的后面跟着用来过滤数据的条件,百分号%是通配符,可以代表多个任何字符,若是下划线_就代表一个任意字符。 实例如下: sql="select * from 表名 where 姓名 like ’%"&request.form("name")&"%’ and 性别 like ’%"&request.form("sex")&"%’ and 电话 like ’%"&request.form("call")&"%’ " 上面这条SQL语句使用了三个模糊查询条件:姓名、性别、电话,当然我们还可以用类似 的方式构造更多的条件。

下面看一个实例:


///模糊查询实例/

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private string getSafestring(string s)
    {
        s = s.Trim();
        s = s.Replace("'","''");
        s = s.Replace("\"", "");
        s = s.Replace("%", "");
        s = s.Replace("--", "");
        s = s.Replace(";", "");
        s = s.Replace("(", "");
        s = s.Replace(")", "");
        s = s.Replace("-", "");
        return s;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ConnectionFactory cf = new ConnectionFactory();
        string sql = "select * from v_topic";
        string key = "";
        if (TextBox1.Text != "")
            key = "topicname like '%" + getSafestring(TextBox1.Text) + "%'";
        if (Checkshowhide.Checked)
        {
            if (TextBox2.Text != "")
            {
                if (key == "")
                    key = "uid like '%" + getSafestring(TextBox2.Text) + "%'";
                else
                    key += " and uid like '%" + getSafestring(TextBox2.Text) + "%'";
            }
            if (RadioButton2.Checked)
            {
                if (key == "")
                    key = "state=1";
                else
                    key += " and state=1";
            }
            if (RadioButton3.Checked)
            {
                if (key == "")
                    key = "state=2";
                else
                    key += " and state=2";
            }
            if (DropDownList1.SelectedItem.Text != "全部时间")
            {
                if (RadioButton4.Checked)
                {
                    if (key == "")
                        key = "emittime > '" + DateTime.Now.AddDays(-int.Parse(DropDownList1.SelectedValue)) + "'";
                    else
                        key += " and emittime > '" + DateTime.Now.AddDays(-int.Parse(DropDownList1.SelectedValue)) + "'";
                }
                else
                {
                    if (key == "")
                        key = "emittime < '" + DateTime.Now.AddDays(-int.Parse(DropDownList1.SelectedValue)) + "'";
                    else
                        key += " and emittime < '" + DateTime.Now.AddDays(-int.Parse(DropDownList1.SelectedValue)) + "'";
                }
            }
            if (RadioButton6.Checked)
            {
                key += " order by " + DropDownList2.SelectedValue;
            }
            else
            {
                key += " order by " + DropDownList2.SelectedValue + " desc";
            }
        }
        if (key != "")
            sql += " where " + key;
        GridView1.DataSource = cf.getTable(sql);
        GridView1.DataBind();
        Label7.Visible = true;
        Label7.Text = "共找到符合条件的数据" + GridView1.Rows.Count + "条";
        Checkshowhide.Checked = false;
    }
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值