读书人

拼凑字符串sql语句查询

发布时间: 2012-08-03 00:12:14 作者: rapoo

拼接字符串sql语句查询
string sql =“”;
if(abc!="")
{
sql=“and dfg(字段名)=abc”;
sql+=select .... ...
}
请教各位高手怎样拼接字符串进行查询,并让的where语句任意加上并执行下去...,具体语法怎么写啊?高手,请教了(asp.net)

[解决办法]
例子

C# code
String strSql=String.Format("SELECT * FROM yourTable WHERE 1=1 ")if(abc!=""){  strSql+=" and 字段名="+abc;或者strSql+=" and 字段名 LIKE '%"+abc+"%'";}
[解决办法]
看这样:
C# code
string sql = "SELECT 字段 FROM 表";if(abc != ""){  sql += " and 条件=" + abc;}//……执行
[解决办法]
失误,纯粹失误:
看这样:
C# code
string sql = "SELECT 字段 FROM 表";if(abc != ""){  sql += " WHERE 字段=" + abc;}//……执行
[解决办法]
探讨
例子
C# code
String strSql=String.Format("SELECT * FROM yourTable WHERE 1=1")if(abc!="")
{
strSql+=" and 字段名="+abc;或者strSql+=" and 字段名 LIKE '%"+abc+"%'";
}

读书人网 >asp.net

热点推荐