急急急!!!

zhurong224 2012-05-31 05:47:14
ViewState.Clear();
string key = " ShowType=" + Request.QueryString["type"] + " and";
if (TextBox1.Text.Length > 0)
key += string.Format(" Address like '%{0}%' and", TextBox1.Text);
if(TextBox2.Text.Length>0&&TextBox3.Text.Length==0)
key += string.Format(" price > {0} and", TextBox2.Text);
if (TextBox2.Text.Length == 0 && TextBox3.Text.Length > 0)
key += string.Format(" price < {0} and", TextBox3.Text);
if (TextBox2.Text.Length > 0 && TextBox3.Text.Length > 0)
key += string.Format(" (price between {0} and {1}) and", TextBox2.Text,TextBox3.Text);
if(DropDownList1.SelectedIndex>0)
key += string.Format(" HouseType_ID={0} and", DropDownList1.SelectedValue);
if(DropDownList2.SelectedIndex>0)
key += string.Format(" Form='{0}' and", DropDownList2.SelectedItem.Text);
if (DropDownList3.SelectedIndex > 0)
key += string.Format(" Qu_ID={0} and", DropDownList3.SelectedValue);
if (DropDownList4.SelectedIndex > 0)
key += string.Format(" HuXing='{0}' and", DropDownList4.SelectedItem.Text);
if (key.Length > 0)
key = key.Substring(0, key.Length - 3);
ViewState["key"] = key;
Bind();
我很想知道为什么key = key.Substring(0, key.Length - 3);这一句为什么要减3 答辩急用
...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dotaxl 2012-06-01
  • 打赏
  • 举报
回复
你的每一个条件后面都有and字符串.最后的目的是用数据库语句 同时也会多余一个and语句。

sub掉最后的3位 也就是去掉了and 这时候得到的才是可以执行的数据库语句.

希望能听明白.
暖枫无敌 2012-06-01
  • 打赏
  • 举报
回复
如果下面的TextBox都没有输入内容时,你的key就不会继续拼凑下去,那么你的查询语句就是
string key = " ShowType=" + Request.QueryString["type"] + " and";
这样,但是多了后面的and,会报语法错误的,所以从SQL字符串中去掉最后3个长度的字符,这样保证SQL语句能正常通过。
孟子E章 2012-05-31
  • 打赏
  • 举报
回复
去掉最后的 and
不去的话
sql 语句不合法了
因为结尾的是 and


111,126

社区成员

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

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

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