注册 登录
编程论坛 ASP技术论坛

拼sql问题

sm105096496 发布于 2007-09-11 16:18, 444 次点击

public DataTable ShowFindscout(string name,string verid)
{
string strline=name;
string [] aryline=null;
string ss="";
aryline = strline.Split(new char[] { ',' });
if(aryline.Length>=2)
{
for (int j = 0; j< aryline.Length; j++)
{
if (j==(aryline.Length-1))
{
ss=ss+"Coupon LIKE '%" +aryline[j]+"%' or "+"Chrcontent LIKE '% or "+aryline[j]+"%' or "+"Keyword LIKE '%"+aryline[j]+"%'";

}
else
{
ss=ss+"Coupon LIKE '%" +aryline[j]+"%' or "+"Chrcontent LIKE '% or "+aryline[j]+"%' or "+"Keyword LIKE '%"+aryline[j]+"%' or ";
}
}
}
else
{
for (int j = 0; j< aryline.Length; j++)
{
ss="Coupon LIKE '%" +aryline[j]+"%' or "+"Chrcontent LIKE '%"+aryline[j]+"%' or "+"Keyword LIKE '%"+aryline[j]+"%'";
}
}
myConn.dbQuery("select Nid,Chrtitle,Chrcontent,Dtappenddate from Life_find where Isopen=1 and verid="+verid+" and ("+ss+") ");
return myConn.FileValue;
}
上面是我写的代码 传值例如:a,b,c
得到 SQL入下

select * from where verid=2 and (Coupon LIKE '%a' or Chrcontent LIKE '%a' Keyword LIKE '%a' or Coupon LIKE '%b' or Chrcontent LIKE '%b' Keyword LIKE '%b' or Coupon LIKE '%c' or Chrcontent LIKE '%c' Keyword LIKE '%c')

现在想把上面的方法改下 传值成 a,b,c|A,B,C
得到SQL如下:请帮我改下

select * from where verid=2 and (Coupon LIKE '%a' or Chrcontent LIKE '%a' Keyword LIKE '%a' or Coupon LIKE '%b' or Chrcontent LIKE '%b' Keyword LIKE '%b' or Coupon LIKE '%c' or Chrcontent LIKE '%c' Keyword LIKE '%c') and (Coupon LIKE '%A' or Chrcontent LIKE '%A' Keyword LIKE '%A' OR Coupon LIKE '%B' or Chrcontent LIKE '%B' Keyword LIKE '%B')

1 回复
#2
yms1232007-09-11 21:54

private string getLIKEQueStr(string tstr)
{
string ss="(";
string[] aryline=tstr.split(',');
for(int j = 0; j< aryline.Length; j++)
{
ss=ss+"Coupon LIKE '%" +aryline[j]+"%' or ";
ss=ss+"Chrcontent LIKE '% or "+aryline[j]+"%' or ";
ss=ss+"Keyword LIKE '%"+aryline[j]+"%' or ";
}
ss=ss.substr(1,ss.length-4);
ss+=")";
return ss;
}

public DataTable ShowFindscout(string name,string verid)
{
string ss="";
string strline=name;
string[] arystr=name.split('|');
for(var i=0;i<arystr.length;i++)
{
ss+=this.getLIKEQueStr(arystr[i])+" and ";
}
ss=ss.substr(1,ss.length-5);
myConn.dbQuery("select Nid,Chrtitle,Chrcontent,Dtappenddate from Life_find where Isopen=1 and verid="+verid+" and ("+ss+") ");
return myConn.FileValue;
}
函数长的时候可以进行分函数写。

1