ASP处理SQL语句的问题(20分)

  • 主题发起人 主题发起人 yangmingnian
  • 开始时间 开始时间
Y

yangmingnian

Unregistered / Unconfirmed
GUEST, unregistred user!
str="I'm a programer"
sql="update TableName set info='"&str&"' where id=1"
conn.execute sql
以上程序执行会出错,是因为str变量中含有单引号'所制
如何处理才能使得当str变量中有单引号也不会出错呢?
 
考虑使用ASP的特殊编码
 
改成:
str="I'm a programer"
sql="update TableName set info="&chr(34)&str&chr(34)&" where id=1"
conn.execute sql
chr(34)="
以上即为: update TableName set info="I'm a programer" where id=1
 
把str改为
str="I"&chr(39)&"m a programmer"
或是
str="I am a programmer" 
再不行了,
不要用UPdate方法,改为rs.Addnew方法,便不会出现了
即:rs.Addnew
rs("Info")=str
rs.update
 
 
我知道了一个更好的方法就是
str="I'm a programer"
sql="update TableName set info='"&str&"' where id=1"
sql=replace(sql,"'","''")
conn.execute sql
也就是把str中的每个单引号用两个单引号代替
这应该是SQL语言的语法,跟Delphi中的语法差不多了
不过VBScript中好象没有replace函数,所以还要自己写一个这样的函数。
 
接受答案了
 
后退
顶部