怎样去空格?!!!!

ChristianBoris 2003-02-24 10:43:55
有三个DropDownList,他们第一个选项都是空字符型,当用SQL语句查询数据库时如果都为空时默认查找的表中的三个空字段,则找不到数据,用SQL语句拼凑太麻烦(如果能拼凑起来也行),有没有一个函数像DELPHI中的TRIM一样可以去空格!!
真诚向各位高手请教!!
...全文
219 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang852963 2003-02-24
  • 打赏
  • 举报
回复
to lin_x_gang(林风) :
你那样如果所有条件为空时,SQL语句变成"select *from table1 where"就会出错。所以最好把SQL语句变成"select *from table1 where 1=1",然后再进行判断,如你写的一样。
ChristianBoris 2003-02-24
  • 打赏
  • 举报
回复
不是吧有7个字段呢!
lbx1979 2003-02-24
  • 打赏
  • 举报
回复
先判断再生成字符串不行吗
lin_x_gang 2003-02-24
  • 打赏
  • 举报
回复
更正:
......以此类推.之后加上
sStrTerm = "where " + sStrTerm;
sStr = sStr + sStrTerm;
^-^.
lin_x_gang 2003-02-24
  • 打赏
  • 举报
回复
string sStr = " select *from table1";
string sStrTerm = "";
if(this.DropDownList1.SelectItem.Text.Trim() != "")
{
sStrTerm = sStrTerm + "cpbid = '" +
this.DropDownList1.SelectItem.Text+"'";
}
if(this.DropDownList2.SelectItem.Text.Trim() != "")
{
if(sStrTerm == "")
{
sStrTerm = sStrTerm + "cpbid = '" +
this.DropDownList2.SelectItem.Text+"'";
}
else
{
sStrTerm = sStrTerm + " and cpbid = '" +
this.DropDownList2.SelectItem.Text+"'";
}
}
......以此类推.
比较笨一点,谁有好办法.
ChristianBoris 2003-02-24
  • 打赏
  • 举报
回复
如果为空的字段太多就判断不了了!因为有WHERE和AND在里面,所以光判断不行,要7!种情况。
QQ86087516 2003-02-24
  • 打赏
  • 举报
回复
判断一下不就搞定了
string sql="select * from table1 where 1=1";
if(**!="")
sql=sql+ " and 条件=×××";
...
...
...

ChristianBoris 2003-02-24
  • 打赏
  • 举报
回复
我的意思是有空格是系统默认也是一个字段,我想让它为空时不执行!例如:
sStr=" select *from table1"+
" where cpbid = '" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbtype ='"+this.DropDownList1.SelectItem.Text+"'"+
" and cpbsubrela ='" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbpopedom ='" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbinvectry ='" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbtradetyp ='" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbregorgn = '" + this.DropDownList1.SelectItem.Text+"'";
当几个条件为空是就跳过不执行比如CPBID=‘’则
sStr=" select *from table1"+
" where cpbtype ='"+this.DropDownList1.SelectItem.Text+"'"+
" and cpbsubrela ='" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbpopedom ='" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbinvectry ='" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbtradetyp ='" + this.DropDownList1.SelectItem.Text+"'"+
" and cpbregorgn = '" + this.DropDownList1.SelectItem.Text+"'";
请教!!
starky 2003-02-24
  • 打赏
  • 举报
回复
表述不清;)
maotin 2003-02-24
  • 打赏
  • 举报
回复
String.Trim(),
klxyz 2003-02-24
  • 打赏
  • 举报
回复
字符串的方法Str.Trim()与Delphi中类似
但不太明白你想干什么 呵呵 你说的太模糊了
fushuming 2003-02-24
  • 打赏
  • 举报
回复
当然有了,也是用Trim()的
ChristinaR 2003-02-24
  • 打赏
  • 举报
回复
真的很简单!:)
ChristianBoris 2003-02-24
  • 打赏
  • 举报
回复
这个问题太简单我已经搞定了!只要设定一个必须输入的框就搞定了!要不WHERE 和 AND不好控制!
chinchy 2003-02-24
  • 打赏
  • 举报
回复
只能一个一个判断,再构造SQL语句,没别的办法.
======================
不要告诉我说用DELPHI不用判断,这是不可能的
hsj178 2003-02-24
  • 打赏
  • 举报
回复
string sStr = " select *from table1 where";
string sStrTerm = "";
if(this.DropDownList1.SelectItem.Text.Trim() != "")
{
sStrTerm = sStrTerm + "cpbid = '" +
this.DropDownList1.SelectItem.Text+"'";
}
//下面的是一样递推的判断
if(this.DropDownList2.SelectItem.Text.Trim() != "")
{
if(sStrTerm == "")
{
sStrTerm = sStrTerm + "cpbid = '" +
this.DropDownList2.SelectItem.Text+"'";
}
else
{
sStrTerm = sStrTerm + " and cpbid = '" +
this.DropDownList2.SelectItem.Text+"'";
}
}
。。。。。
sStr = sStr + sStrTerm;

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧