如:
string sql = "select kb,jz,lb,(sum(case when convert(varchar(20),expire,23)>='"
+ this.tbstart.Text.Trim() + "'or expire is null or expire='' ) then 1 else 0 end)+sum(case when convert(varchar(20),expire,23)>='" + this.tbover.Text.Trim() + "' or expire is null or expire='' ) then 1 else 0 end))/2 as 总人数,
sum(case when start between '" + this.tbstart.Text + "' and '" + this.tbover.Text + "') then 1 else 0 end) as 辞职总人数如上面的语句,太长了,后面还有。
请问大家,有没有方法简单一点的,比如把一句一句分开,在select的时候在调用呢?
谢谢大家了。
string sql = "select kb,jz,lb,(sum(case when convert(varchar(20),expire,23)>='"
+ this.tbstart.Text.Trim() + "'or expire is null or expire='' ) then 1 else 0 end)+sum(case when convert(varchar(20),expire,23)>='" + this.tbover.Text.Trim() + "' or expire is null or expire='' ) then 1 else 0 end))/2 as 总人数,
sum(case when start between '" + this.tbstart.Text + "' and '" + this.tbover.Text + "') then 1 else 0 end) as 辞职总人数如上面的语句,太长了,后面还有。
请问大家,有没有方法简单一点的,比如把一句一句分开,在select的时候在调用呢?
谢谢大家了。
using System.Text;StringBuilder sb = new StringBuilder();
sb.Append("select kb,jz,lb,");
sb.Append("(sum(case when convert(varchar(20),expire,23)>='"+this.tbstart.Text.Trim());
sb.Append("...");
//自己慢慢拼吧string sql = sb.ToString();
sum(case when start between '{2}' and '{3}') then 1 else 0 end) as 辞职总人数";string sql = string.format(sql,this.tbstart.Text.Trim(), this.tbover.Text.Trim(), this.tbstart.Text, this.tbover.Text);这样味道就不同了,而且效率也要好于字符串拼接 前面我提到的sqlparam也是一种方案 可维护性要强于lz写的
sum(case when start between '{2}' and '{3}') then 1 else 0 end) as 辞职总人数";sql = string.format(sql,this.tbstart.Text.Trim(), this.tbover.Text.Trim(), this.tbstart.Text, this.tbover.Text);变量类型声明了两遍 罪过.....改进一下
写参数化的sql语句,写在.sql的文件中,
vs还提供只能提示啊,
然后作为资源加载,直接读取就可以了
http://topic.csdn.net/u/20110705/00/c42bf185-ad49-4731-b5cf-608835297cd3.html
这个不是你的主号吧,我记得很早前跟这个id很相似就是一个星星要不就是两个星星了 在.net还是web版的
sb.Append("select * from table where 1=1");
if(条件1满足)
{
sb.Append(" and a=6 ");
}
if(条件2满足)
{
sb.Append(" and b=8 ");
}
string sql = sb.ToString();