sql = "select * from address where (name like = '%" + txtName.Text + "%' or '" + txtName.Text + "' = '' ) and (unit like = '%" + txtUnit.Text + "%' or '" + txtUnit.Text + "' = '' )"

解决方案 »

  1.   

    if (txtName.Text != "" && txtUnit.Text != "")
    {
      string sqlStr = "Select name,unit from address where name like '%" + txtName.Text + "%' and unit like '%" + txtUnit.Text + "%'";
    }
    else
    {
      string sqlStr = "Select name,unit from address where name like '%" + txtName.Text + "%' or unit like '%" + txtUnit.Text + "%'";
      

  2.   

    楼上的语句可以在数据库中执行吗?是不是分成两个sql
      

  3.   

    set @txtname=txtName.text
    set @txtunit=txtUnit.text
    select * from address where (name like '%' + @txtname + '%' and unit like +'%' + @txtunit+ '%'+ ) or (unit like +'%' + @txtunit+  '%'+ and @txtname = '' ) or (name like + '%' + @txtname+  '%'+ and @txtunit= '' )
      

  4.   

    听老大的,没错!
    你在数据库中没办法执行吧,因为你用了两个textbox了,库里没有:)
      

  5.   

    还是思归强啊!
    admire to death...
      

  6.   

    不可能在数据库里查询分析器中执行的,因为textbox不是sql server 的成员,它是asp.net的一部分
      

  7.   

    sql = "select * from address where (name like  '%" + txtName.Text + "%' or '" + txtName.Text + "' = '' ) and (unit like  '%" + txtUnit.Text + "%' or '" + txtUnit.Text + "' = '' )"//把思归写的sql语句中like后面的 = 去掉
      

  8.   

    这个应该先判断两个输入框的数据是否为空,然后选择相对应的sql语句
      

  9.   

    CREATE PROCEDURE QueryAddressByNameUnit
    (
    @Name varchar(50),
    @Unit varchar(200)
    )
    as 
    select * from T_AddressList 
    where (([Name] like '@Name%' or [Name] is null) and (Unit like '@Unit%' or Unit is null))
    GO
      

  10.   

    也可以,调用一个含有两个参数的存储过程,在存储过程内写SQL语句。