LINQ实现多个条件的查询

    实现查询多个条件的功能,就是拼接字符串

  

 

 protected void btnSearch_Click(object sender, EventArgs e)     //直接连接数据库查询
        {
                string sql = "select * from Student s where 1=1";
                if (!string.IsNullOrEmpty(txtName.Text))
                    sql += "and s.s_name='" + txtName.Text.Trim() + "'";
                if (!string.IsNullOrEmpty(txtSex.Text))
                    sql += "and s.s_sex='" + txtSex.Text.Trim() + "'";
                if (!string.IsNullOrEmpty(txtDep.Text))
                    sql += "and s.d_num='" + txtDep.Text.Trim() + "'";
                if (!string.IsNullOrEmpty(txtClass.Text))
                    sql += "and s.c_num='" + txtClass.Text.Trim() + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Child");
                GridView1.DataSource = ds.Tables["Child"];
                GridView1.DataBind();
        }

 

   protected void btnSearchL_Click(object sender, EventArgs e)    //LINQ查询
        {
            StudentCont.StudentDataContext stu = new StudentCont.StudentDataContext();
            var q = from s in stu.Student select s;
            if (!string.IsNullOrEmpty(txtName.Text))
               q=q.Where(t => t.s_name == txtName.Text.Trim());
            if (!string.IsNullOrEmpty(txtSex.Text))
                q=q.Where(t => t.s_sex == txtSex.Text.Trim());
            if (!string.IsNullOrEmpty(txtDep.Text))
                q=q.Where(t => t.d_num == txtDep.Text.Trim());
            if (!string.IsNullOrEmpty(txtClass.Text))
                q=q.Where(t => t.c_num == txtClass.Text.Trim());
            GridView2.DataSource = q;
            GridView2.DataBind();
        }

 

以上代码是参考别人的写出来的,也贴出来给大家参考。

原创:http://blog.csdn.net/q107770540/article/details/5724013#comments

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值