access2003数据库,注意,不是sqlserver,我来这里问是因为这里人多点:)
我在C#里写的sql语句string strSQL = "select * from tableA where 1 = 1 and CompanyName = '"+ strCompanyName + "'";我现在需要对变量strCompanyName的值做模糊查询
请哪位帮我改一下上面的语句???我写了好多种,都不对,郁闷死了。

解决方案 »

  1.   

    CompanyName like %strCompanyName %
      

  2.   

    string strSQL = "select * from tableA where 1 = 1 and CompanyName LIKE '%"+ strCompanyName + "%'";
      

  3.   


    "select * from tableA where 1 = 1 and CompanyName LIKE ''%"+ strCompanyName + "%''"
      

  4.   

    where charindex(strCompanyName,CompanyName )>0
      

  5.   

    1楼是对的。
    在C#里面:
    string strSQL = "select * from tableA where 1 = 1 and CompanyName like '%"+ strCompanyName + "%'";
    或者这个更直观点:
    string.Format("select * from tableA where 1 = 1 and CompanyName like '%{0}%'", strCompanyName);建议,养成习惯把要的字段一个个列出来会更好点,而不是偷懒用*;
    where 1=1,你是要做多个条件的查询,所以拼接sql语句么,这个语句没什么必要啊?