请教C#中的一个SQL语句~!
部分代码如下:
......
  string sql ="select * from table where id ='"+new_id"'"
   if(名称.text!=null) 
        sql = sql + "..."//想在这在上面的sql基础上再加个where条件,比如 where 项目名称 = '开发'
....
请教下这个该怎么写呢?
谢谢大家~!

解决方案 »

  1.   

      string sql ="select * from table where id ='"+new_id"'" 
      if(名称.text!=null) 
            sql = sql + " and 项目名称 = '开发' ";
      

  2.   

    就这样啊.你的不是已经写出来了吗string sql ="select * from table where id ='"+new_id"'" 
      if(!string.IsNullOrEmpty(名称.text.Trim())) 
            sql = sql + " AND 名称='"+名称.text.Trim() + "'";
      

  3.   

    我写的是 sql = sql +"where 项目名称 ='开发'";
    可是不对啊?
    在线请教~~!~!
      

  4.   

    我写的是 sql = sql +"where 项目名称 ='开发'";
    可是不对啊?
    在线请教~~!~!
      

  5.   

    sql+="and 项目名称=‘开发’"
    或者
    sql=sql+"and 项目名称=‘开发’"
      

  6.   

    where 改成 and 
    不然已经两个where了???呵呵
      

  7.   

    把where 改成 and,楼上已经回复了,你应该好好看看啊
      

  8.   

    string sql ="select * from table where id ='"+new_id"' " 
      if(名称.text!=null) 
            sql = sql + " and 项目名称 = '开发' ";就是用and连接就可以了,但是注意要留空格,否则执行时会报错。
      

  9.   

    多个查询条件连接不能用WHERE ,用AND或者OR
      

  10.   

    用 append
    string sql ="select * from table where id ='"+new_id"'" 
      if(名称.text!=null) 
            sql.append("...")
      

  11.   


    楼上你确定string后面带又append属性么?应该是StringBuilder里面的吧
    楼主的问题上面的各位已经给出答案了,不应该又两个where的。
      

  12.   

    ...... 
      string sql ="select * from table ";
      if(名称.text!=null) 
            sql = sql + " where id ='"+new_id+"' and 项目名称 = '开发'";
    .... 
    楼主试试可是这意思?
      

  13.   

    string sql ="select * from table "; 
      if(名称.text!=null) 
            sql += "  and 项目名称 = '开发'"; 
      

  14.   

    高手来了!!!!!!!!!!
     没戏。可以注入的SQL
      

  15.   

      string sql ="select * from table where id ='"+"new_id'" ;
      if( 项目名称 .text!="")

            sql += " and 项目名称 = '" + 项目名称 .Text + "'";
    }
      

  16.   

    针对你说的,我觉得你可以这样子写:
    stringbuilder strbu=new stringbuilder();
    strbu.append("select * from table where id='"+new_id+"'");
    if(名称.text!=null)
    {
        strbu.append(" and 项目名称='开发'");
    }
    然后你可以把它转化成String
    string sql=strbu.tostring();
      

  17.   

    大家看看,我是这样写的StringBuilder SQL = new StringBuilder();
    SQL.Append("select * from table where 1=1 AND id ='"+new_id"' ");
    if(名称.text.trim()!=String.empty) 
    {
        SQL.Append("AND 项目名称 = '开发' "};
    }
    如果被SQL注入的话会有问题的,最好用参数把条件加进去,
      

  18.   

    string sql ="select * from table where id ='"+new_id"'" 
      if(名称.text.trim().length!=0) 
            sql += " and ......."//想在这在上面的sql基础上再加个where条件,比如 where 项目名称 = '开发' 
      

  19.   


    string sql ="select * from table where id ='"+new_id"'"
      if(名称.text!=null)
            sql = sql + " where name='啥'"
      

  20.   

    string sql ="select * from table where id ='"+new_id"'"first of all,Suppose that condition is true,So you can write sql like this
    String strSQL="SELECT * FROM [table] WHERE 1=1 "then you can construct your sql accord to your condition
    if(name.text!=null){
     strSQL+=" and name like '%"+name+"%'";

      

  21.   


    写明了就是:
    sql ="select * from table where id ='"+new_id"' and 项目名称 = '开发' "  ;
    ---------
     if(名称.text!=null) 
            sql = sql +  " and 项目名称 = '开发' ";