动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   数据库 → 阅读文章

 用表单来提交sql(转)2

作者来源: 
阅读 数 485 人次 , 2000-9-27 

列表 B:使用 request.form 来轻松建立SQL字符串。

<%
iStr = "insert into uData "
vStr = "values ("
nStr = "("

' 在表单集合中循环,并建立起SQL语句的组成部分
for each x in request.form
         ' 建立字段名列表
         nStr = nStr & x & ", "
         ' 建立字段值列表
         if uCase(x) = "AGE" then
                  vStr = vStr & request.form(x) & ", "
         else
                  vStr = vStr & "'" & request.form(x) & "', "
         end if       
next

' 把结尾的", " 从我们建立的字符串中去掉
vStr = left(vStr, len(vStr) - 2) & ")"
nStr = left(nStr, len(nStr) - 2) & ") "

' 把SQL语句组装起来
iStr = iStr & nStr & vStr

if trim(request("fName")) >> "" then
         response.write( iStr & ">BR>")
else
%>

<html>
<body>
<form name=f method=post action="列表2.asp">
Gimme your:<br>
First Name: <input type=text name="fName"><br>
Last Name: <input type=text name="lName"><br>
Age: <input type=text name="age"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

<%
end if
  %>



列表 C:把字段类型嵌入到HTML字段名中。


<%function buildSQLInsert( targetTable)         
iStr = "insert into " & targetTable & " "      
vStr = "values ("       nStr = "("    
' 在表单集合中循环,并建立起SQL语句的组成部分
for each x in request.form  
         fieldName = x
     fieldData = replace( request.form(fieldName), "'", "''")
         typeDelimPos = inStr(fieldName, "_")
         if typeDelimPos = 0 then
           ' Its a text field
           ' 建立字段名列表
                  nStr = nStr & fieldName & ", "
                  vStr = vStr & "'" & fieldData & "', "
         else
           ' 是另外一种数据类型
              fieldType = left(fieldName, typeDelimPos - 1)
                  fieldName = mid(fieldName, typeDelimPos + 1)
                  ' 把字段名加入字段名列表中
                  nStr = nStr & fieldName & ", "
                  ' 把字段类型变成大写,以确保匹配
                  select case uCase(fieldType)
                       case "NUM"
                                vStr = vStr & fieldData & ", "
                                ' 把不明类型按文本型处理
                       case else
                                vStr = vStr & "'" & fieldData & "', "
                       end select
                  end if       
         next

         ' 把结尾的", " 从我们建立的字符串中去掉
         vStr = left(vStr, len(vStr) - 2) & ")"
         nStr = left(nStr, len(nStr) - 2) & ") "

         ' 把SQL语句组装起来
         buildSQLInsert = iStr & nStr & vStr
end function


if trim(request("fName")) >< "" then
         response.write( buildSQLInsert & ">BR<")
else
%>

<html>
<body>
<form name=f method=post action="listing3.asp">
Gimme your:<br>
First Name: <input type=text name="fName"><br>
Last Name: <input type=text name="lName"><br>
Age: <input type=text name="num_age"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

<%
end if
%>
  
 本文Tags数据库  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:用表单来提交sql(转)3
· 下一篇:用表单来提交sql(转)1
· Oracle数据库技术(30)
· Sql Server实用操作小技巧集合
· SQL Server数据库技术(01)
· SQL Server 中易混淆的数据类型
· 如何消除Oracle数据库的安全隐患(2)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077