用表单来提交sql - 2

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

 1   
 2iStr = "insert into uData "   
 3vStr = "values ("   
 4nStr = "("   
 5  
 6' 在表单集合中循环,并建立起SQL语句的组成部分   
 7for each x in request.form   
 8' 建立字段名列表   
 9nStr = nStr & x & ", "   
10' 建立字段值列表   
11if uCase(x) = "AGE" then   
12vStr = vStr & request.form(x) & ", "   
13else   
14vStr = vStr & "'" & request.form(x) & "', "   
15end if   
16next   
17  
18' 把结尾的", " 从我们建立的字符串中去掉   
19vStr = left(vStr, len(vStr) - 2) & ")"   
20nStr = left(nStr, len(nStr) - 2) & ") "   
21  
22' 把SQL语句组装起来   
23iStr = iStr & nStr & vStr   
24  
25if trim(request("fName")) >> "" then   
26response.write( iStr & ">BR>")   
27else   
 1<html>
 2<body>
 3<form action="列表2.asp" method="post" name="f">   
 4Gimme your:<br/>   
 5First Name: <input name="fName" type="text"/><br/>   
 6Last Name: <input name="lName" type="text"/><br/>   
 7Age: <input name="age" type="text"/><br/>
 8<input type="submit" value="Submit"/>
 9</form>
10</body>
11</html>
1   
2end if   

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

 1function buildSQLInsert( targetTable)   
 2iStr = "insert into " & targetTable & " "   
 3vStr = "values (" nStr = "("   
 4' 在表单集合中循环,并建立起SQL语句的组成部分   
 5for each x in request.form   
 6fieldName = x   
 7fieldData = replace( request.form(fieldName), "'", "''")   
 8typeDelimPos = inStr(fieldName, "_")   
 9if typeDelimPos = 0 then   
10' Its a text field   
11' 建立字段名列表   
12nStr = nStr & fieldName & ", "   
13vStr = vStr & "'" & fieldData & "', "   
14else   
15' 是另外一种数据类型   
16fieldType = left(fieldName, typeDelimPos - 1)   
17fieldName = mid(fieldName, typeDelimPos + 1)   
18' 把字段名加入字段名列表中   
19nStr = nStr & fieldName & ", "   
20' 把字段类型变成大写,以确保匹配   
21select case uCase(fieldType)   
22case "NUM"   
23vStr = vStr & fieldData & ", "   
24' 把不明类型按文本型处理   
25case else   
26vStr = vStr & "'" & fieldData & "', "   
27end select   
28end if   
29next   
30  
31' 把结尾的", " 从我们建立的字符串中去掉   
32vStr = left(vStr, len(vStr) - 2) & ")"   
33nStr = left(nStr, len(nStr) - 2) & ") "   
34  
35' 把SQL语句组装起来   
36buildSQLInsert = iStr & nStr & vStr   
37end function   
38  
39  
40if trim(request("fName")) >< "" then   
41response.write( buildSQLInsert & ">BR<")   
42else   
 1<html>
 2<body>
 3<form action="listing3.asp" method="post" name="f">   
 4Gimme your:<br/>   
 5First Name: <input name="fName" type="text"/><br/>   
 6Last Name: <input name="lName" type="text"/><br/>   
 7Age: <input name="num_age" type="text"/><br/>
 8<input type="submit" value="Submit"/>
 9</form>
10</body>
11</html>
1   
2end if   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus