怎样实现多条件搜索,求帮忙啊~~

装作睡不着 2014-03-26 10:00:20
这是我做的页面,和后台代码,怎样才能实现多条件搜索,但是不用书写太多的SQL语句,按照我的后天代码,四个条件,如果任选几个条件搜索的话,可能要写十多条SQl语句。求大神帮忙!!
...全文
345 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
webdiyer 2014-03-26
  • 打赏
  • 举报
回复
好多年不写sql了,如果楼主用Entity Frameowrk的话,就简单多了: var students=dbContext.Students.AsQueryable(); if(!string.IsNullOrWhiteSpace(xhbox.Text)){ students=students.Where(s=>s.XH.Contains(xhbox.Text)); } if(!string.IsNullOrWhiteSpace(namebox.Text)){ students=students.Where(s=>s.Name.Contains(namebox.Text)); } ..... ===================== http://www.webdiyer.com
  • 打赏
  • 举报
回复
不过楼上的哥们给了我提醒,我之前写的那个里面没有模糊查询,可以改下,改成:
select 你那几个列我就不写了 
from Student where (StudentID=''or StudentID like '%'+@StudentID+'%')
and ([Name]='' or [Name] like '%'+@[Name]+'%')
  • 打赏
  • 举报
回复
引用 5 楼 still_melody 的回复:
五个裤衩连多条件都没做过吗?

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind("");
        }
    }
    protected void bind(string condition)
    {
        string sql = "select * from table where 1=1 " + condition;
    }
    protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
        string condition = "";
        if (!string.IsNullOrEmpty(Textid.Text))
        {
            condition += " and studentid =" + Textid.Text + "%'";//用+=
        }
        if (!string.IsNullOrEmpty(TextName.Text))
        {
            condition += " and name like '%" + TextName.Text + "%'";
        }
        //下面条件省略
    }
我在SQL里面多条件了,你在代码里面多条件而已
still_melody 2014-03-26
  • 打赏
  • 举报
回复
五个裤衩连多条件都没做过吗?

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind("");
        }
    }
    protected void bind(string condition)
    {
        string sql = "select * from table where 1=1 " + condition;
    }
    protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
        string condition = "";
        if (!string.IsNullOrEmpty(Textid.Text))
        {
            condition += " and studentid =" + Textid.Text + "%'";//用+=
        }
        if (!string.IsNullOrEmpty(TextName.Text))
        {
            condition += " and name like '%" + TextName.Text + "%'";
        }
        //下面条件省略
    }
  • 打赏
  • 举报
回复
貌似Name是个关键字啊,看来你这个数据库字段设计的不太好,上面的修改下:
select 你那几个列我就不写了 
from Student where (StudentID=''or StudentID=@StudentID)
and ([Name]='' or [Name]=@[Name])
  • 打赏
  • 举报
回复
引用 2 楼 u013362278 的回复:
引用 1 楼 yb871201 的回复:
加一个默认条件1=1,之后用键值对吧。 遍历键值对。来控制查询条件。
不懂,能不能写一段代码。

select 你那几个列我就不写了 
from Student where (StudentID=''or StudentID=@StudentID)
and (Name='' or Name=@Name)
以此类推,有几个就用几个作为条件
装作睡不着 2014-03-26
  • 打赏
  • 举报
回复
引用 1 楼 yb871201 的回复:
加一个默认条件1=1,之后用键值对吧。 遍历键值对。来控制查询条件。
不懂,能不能写一段代码。
Kylor 2014-03-26
  • 打赏
  • 举报
回复
加一个默认条件1=1,之后用键值对吧。 遍历键值对。来控制查询条件。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧