日期:2014-05-17  浏览次数:20438 次

拼接字符串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+"%'";
}