请教C#中的一个SQL语句~!
部分代码如下:
......
string sql ="select * from table where id ='"+new_id"'"
if(名称.text!=null)
sql = sql + "..."//想在这在上面的sql基础上再加个where条件,比如 where 项目名称 = '开发'
....
请教下这个该怎么写呢?
谢谢大家~!
部分代码如下:
......
string sql ="select * from table where id ='"+new_id"'"
if(名称.text!=null)
sql = sql + "..."//想在这在上面的sql基础上再加个where条件,比如 where 项目名称 = '开发'
....
请教下这个该怎么写呢?
谢谢大家~!
if(名称.text!=null)
sql = sql + " and 项目名称 = '开发' ";
if(!string.IsNullOrEmpty(名称.text.Trim()))
sql = sql + " AND 名称='"+名称.text.Trim() + "'";
可是不对啊?
在线请教~~!~!
可是不对啊?
在线请教~~!~!
或者
sql=sql+"and 项目名称=‘开发’"
不然已经两个where了???呵呵
if(名称.text!=null)
sql = sql + " and 项目名称 = '开发' ";就是用and连接就可以了,但是注意要留空格,否则执行时会报错。
string sql ="select * from table where id ='"+new_id"'"
if(名称.text!=null)
sql.append("...")
楼上你确定string后面带又append属性么?应该是StringBuilder里面的吧
楼主的问题上面的各位已经给出答案了,不应该又两个where的。
string sql ="select * from table ";
if(名称.text!=null)
sql = sql + " where id ='"+new_id+"' and 项目名称 = '开发'";
....
楼主试试可是这意思?
if(名称.text!=null)
sql += " and 项目名称 = '开发'";
没戏。可以注入的SQL
if( 项目名称 .text!="")
{
sql += " and 项目名称 = '" + 项目名称 .Text + "'";
}
stringbuilder strbu=new stringbuilder();
strbu.append("select * from table where id='"+new_id+"'");
if(名称.text!=null)
{
strbu.append(" and 项目名称='开发'");
}
然后你可以把它转化成String
string sql=strbu.tostring();
SQL.Append("select * from table where 1=1 AND id ='"+new_id"' ");
if(名称.text.trim()!=String.empty)
{
SQL.Append("AND 项目名称 = '开发' "};
}
如果被SQL注入的话会有问题的,最好用参数把条件加进去,
if(名称.text.trim().length!=0)
sql += " and ......."//想在这在上面的sql基础上再加个where条件,比如 where 项目名称 = '开发'
string sql ="select * from table where id ='"+new_id"'"
if(名称.text!=null)
sql = sql + " where name='啥'"
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+"%'";
}
写明了就是:
sql ="select * from table where id ='"+new_id"' and 项目名称 = '开发' " ;
---------
if(名称.text!=null)
sql = sql + " and 项目名称 = '开发' ";