⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 444.html

📁 关于jsp的一些好文章 主要介绍一些关于JSP的应用技巧方面的东西
💻 HTML
字号:

<STYLE type=text/css>
<!--
body,td { font-size:9pt;}
hr { color: #000000; height: 1px}
-->
</STYLE>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD><TITLE>论坛精华 >> SQL之家 >> 关于SQL语句中的引号问题(VB&amp;VBScript)</title>
</head>
<body >

<p><IMG SRC="../image/jsp001_middle_logo.gif" WIDTH="180" HEIGHT="60" BORDER=0 ALT=""></p>

<table width=100% bgcolor="#cccccc" align=center cellpadding="2" cellspacing="0" border=1 bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr bgcolor="#EFF8FF"><td>
<a href=http://www.jsp001.com/list_thread.php?int_attribute=4>论坛精华</a>
>> <a href=http://www.jsp001.com/list_thread.php?forumid=45&int_attribute=4>SQL之家</a>
>> 关于SQL语句中的引号问题(VB&amp;VBScript) [<a href=http://www.jsp001.com/forum/showthread.php?goto=newpost&threadid=444>查看别人的评论</a>]<br>

<hr><p>由 amtd 发布于: 2001-02-15 09:30</p><p><img src="images/icons/icon1.gif" alt="Post" border=0> </p><p>关于SQL语句中的引号问题(VB&amp;VBScript)<br><br>    最近在网上看到不少网友因为在写 SQL语句时因为引号引起的问题,我在刚开始用VB的<br>时候也经常犯这样或那样的错误,这次将使用的经验写出来与大家共享。<br><br>    * 举例时以 VB6.0为依据;<br>    * VB与VBScript不同的地方将分别说明;<br>    * 数据库连结用 ADO。<br><br>    要点:<br>    在 VB&amp;VBScript中,标记字符串变量内容用双引号,用两个连续的双引号表示字符串中<br>的双引号;<br>    在 SQL语法中,标记字符串变量内容用单引号,用两个连续的单引号表示字符串中的单<br>引号。<br><br>    在 VB&amp;VBScript中访问数据库的时候最常用的是以下两种语句:<br>    1、选择查询(select),返回结果集;<br>    2、动作查询(update、insert、delete等),无结果集。<br><br>    无论哪一种语句,经常需要根据用户的输入来构造符合 SQL语法的字符串,提交给连结<br>对象或结果集对象来执行(open或 execute方法),在 VB&amp;VBScript中,标记字符串变量内<br>容用双引号,如<br>    strSql="select * from UserList"<br>在 SQL语法中,标记字符串变量内容用单引号,如:<br>    select * from UserList where UserName='MouseFly'<br><br>    如果程序的窗口(浏览器的页面)上有两个文本框用于让用户输入登记注册的用户信息<br>或者是查询条件,假设用户输入了“MouseFly”、“1234”,需要登记到数据库的一个用户<br>注册表 UserList中,则SQL字符串应如下组织:<br>    strSql="insert into UserList (UserName,Password) values (" &amp; _<br>           "'" &amp; me.txtUserName.text &amp; "'," &amp; _<br>           "'" &amp; me.txtPassword.text &amp; "')"<br>生成的结果为:<br>    insert into UserList (UserName,Password) values ('MouseFly','1234')<br>但是如果用户输入中包含了单引号,如“12'3”,按照上面的组织方法则为:<br>    insert into UserList (UserName,Password) values ('MouseFly','12'3')<br>这时再执行的时候,会提示 SQL语法错误,原因就在于“12'3”中的单引号,由于在 SQL语<br>法中,标记字符串变量内容用单引号,因此“'12'3'”无法被正确识别,所以在组织字符串<br>的时候要考虑将用户输入的单引号替换成两个连续的单引号,此时要用到Replace函数:<br>    strSql="insert into UserList (UserName,Password) values (" &amp; _<br>           "'" &amp; replace(me.txtUserName.text,"'","''") &amp; "'," &amp; _<br>           "'" &amp; replace(me.txtPassword.text,"'","''") &amp; "')"<br>结果为:<br>    insert into UserList (UserName,Password) values ('MouseFly','12''3')<br>如果使用的是VBScript则me.txtUserName.text和me.txtPassword.text要换成相应的浏览器<br>提交的内容,如request.form("UserName")和request.form("Password")。<br>    如果赋值的字段类型是数字型的,则要去掉字符串两端的单引号,假设Password字段是<br>数值型,并且用户输入的是“1234”,则(此处略去数字输入校验):<br>    strSql="insert into UserList (UserName,Password) values (" &amp; _<br>           "'" &amp; me.txtUserName.text &amp; "'," &amp; _<br>           me.txtPassword.text &amp; ")"<br>生成的结果为:<br>    insert into UserList (UserName,Password) values ('MouseFly',1234)<br><br>    如果字段类型是日期型,在 ADO中和处理字符串差不多,只不过要注意日期型字段的格<br>式,最好在组织 SQL字符串的时候对日期进行格式化保证年份是4位,如:<br>    strSql="insert into UserList (UserName,Password,BirthDay) values (" &amp; _<br>           "'" &amp; me.txtUserName.text &amp; "'," &amp; _<br>           me.txtPassword.text &amp; "," &amp; _<br>           "'" &amp; format(me.txtBirth.text,"yyyy-mm-dd") &amp; "')"<br>生成的结果为:<br>    insert into UserList (UserName,Password,BirthDay) values <br>    ('MouseFly',1234,'1975-01-15')<br>如果使用的是VBScript,对日期进行格式化使用FormatDatetime函数。<br><br><br> <br><br>__________________<br><font color=red>真实源于生活! </font><br>请访问我们的网站: <br>(VB爱好者乐园) <br><a href="http://www.vbgood.com" target=_blank>http://www.vbgood.com</a><br><a href="http://www.d1vb.com" target=_blank>http://www.d1vb.com</a><br><a href="http://61.128.97.225/vbgood/index.asp" target=_blank>http://61.128.97.225/vbgood/index.asp</a><br>拥有1800多个资料! </p></td>
  </tr>
</table>

<p>
<CENTER><a href="http://www.jsp001.com/forum/newreply.php?action=newreply&threadid=444">点这里对该文章发表评论</a></CENTER>
<p>该文章总得分是 <font color=red>0</font> 分,你认为它对你有帮助吗?
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=444&intVote=4","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>非常多</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=444&intVote=2","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>有一些</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=444&intVote=1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>无帮助</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=444&intVote=-1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>是灌水</a>](<font color=red>0</font>) </p>
<script language="javascript" src="http://www.jsp001.com/include/read_thread_script.php?threadid=444"></script>
<p><CENTER>
Copyright &copy; 2001 - 2009 JSP001.com . All Rights Reserved <P>

<IMG SRC="../image/jsp001_small_logo.gif" WIDTH="85" HEIGHT="30" BORDER=0 ALT="">
</CENTER></p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -