佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1487|回复: 7

最节俭的sql statement写法

[复制链接]
发表于 10-6-2006 10:20 AM | 显示全部楼层 |阅读模式
我知道这段sql statement不够完善... 因为这段sql statement可以存在很多可能性,想知道最节俭的写法....

请大家赐教

Dim name as string = Request.QueryString("name")
Dim age as string = Request.QueryString("age")
Dim tel1 as string = Request.QueryString("tel1")


      Str = "SELECT name FROM customer WHERE "

      If NOT name = "" then
         Str = Str + "postcode = '"+ postcode +"'"
      End If         
      
      If NOT age = "" then
         Str = Str + "AND age = '"+ age +"'"           
      End If
      
      If NOT tel1 = "" then
         Str = Str + "AND tel1 = '"+ tel1 +"'"           
      End If
回复

使用道具 举报


ADVERTISEMENT

发表于 11-6-2006 03:13 AM | 显示全部楼层
如果你的 name 是空的... 那么你的 code 就会出现问题了.
你的方法已经很好, 再想想看怎么安排 where 和 and 吧...
回复

使用道具 举报

发表于 14-6-2006 02:13 PM | 显示全部楼层
Dim name as string = Request.QueryString("name")
Dim age as string = Request.QueryString("age")
Dim tel1 as string = Request.QueryString("tel1")

Str = "SELECT name FROM customer WHERE 1 "

If name <> "" then
   Str = Str & "AND postcode = '" & postcode & "'"
End If         
      
If age <> "" then
   Str = Str &  "AND age = '" & age & "'"           
End If
      
If tel1 <> "" then
   Str = Str + "AND tel1 = '" & tel1 & "'"           
End If
回复

使用道具 举报

发表于 16-6-2006 09:57 AM | 显示全部楼层
Dim name as string = Request.QueryString("name")
Dim age as string = Request.QueryString("age")
Dim tel1 as string = Request.QueryString("tel1")

Str = "SELECT name FROM customer WHERE 1<>2 "

If name <> "" then
   Str = Str & "AND postcode = '" & postcode & "'"
End If         
      
If age <> "" then
   Str = Str &  "AND age = '" & age & "'"           
End If
      
If tel1 <> "" then
   Str = Str + "AND tel1 = '" & tel1 & "'"           
End If
回复

使用道具 举报

 楼主| 发表于 22-6-2006 01:57 AM | 显示全部楼层
原帖由 笨陈 于 14-6-2006 02:13 PM 发表
Dim name as string = Request.QueryString("name")
Dim age as string = Request.QueryString("age")
Dim tel1 as string = Request.QueryString("tel1")

Str = "SELEC ...


WHERE 1??  可以这样的吗?
回复

使用道具 举报

发表于 23-6-2006 02:19 AM | 显示全部楼层
应该用 kennynjc 的方法, 因为 WHERE 1 的方法会导致 sql injection.
另外, 最好把 where 和 and 转成 variable.
回复

使用道具 举报

Follow Us
发表于 15-9-2006 01:36 PM | 显示全部楼层
where (1=1)
回复

使用道具 举报

发表于 20-9-2006 05:44 PM | 显示全部楼层
原帖由 goatstudio 于 23-6-2006 02:19 AM 发表
应该用 kennynjc 的方法, 因为 WHERE 1 的方法会导致 sql injection.
另外, 最好把 where 和 and 转成 variable.

要小心类似的sql, 尤其是牵涉到system user 的query,会有安全漏洞哦。If I input "myvalue OR 1=1 OR ", then will somehow cause your query to return all records or able to login with any username. If can parameterize then will be better.
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-9-2024 07:14 PM , Processed in 0.163631 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表