直播中
<%
function buildSQLInsert( targetTable, omitFields)
iStr = "insert into " & targetTable & " "
vStr = "values ("
nStr = "("
' 在表單集合中循環(huán),并建立起SQL語句的組成部分
for each x in request.form
fieldName = uCase(x)
' 判斷字段是否被省略?
if inStr(uCase(omitFields),x) = 0 then
fieldData = replace(request.form(fieldName), _
"'", "''")
' 如果沒有數(shù)據(jù),就插入 NULL
if trim(fieldData) = "" then
fieldData = "NULL"
vStr = vStr & fieldData & ", "
nStr = nStr & fieldName & ", "
else
typeDelimPos = inStr(fieldName, "_")
if typeDelimPos = 0 then
' 是文本字段
' 建立字段名列表
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
end if
end if
next
' 把結(jié)尾的", " 從我們建立的字符串中去掉
vStr = left(vStr, len(vStr) - 2) & ")"
nStr = left(nStr, len(nStr) - 2) & ") "
' 把SQL語句整合起來
buildSQLInsert = iStr & nStr & vStr
end function
if trim(request("fName")&request("lname")&request("age")) <> "" then
response.write( buildSQLInsert("") & "<BR<")
response.write( buildSQLInsert("NUM_AGE") & "<BR>")
response.write( buildSQLInsert("lname,fname") & "<BR>")
response.write( buildSQLInsert("mycheckbox,fname") &
=<"<BR<")
else
%>
<html>
<body>
<form name=f method=post action="列表4.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="checkbox" value="Y" name="MyCheckBox"<Do you want this checked?<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<%
end if
%>
列表 D: buildSQLInsert函數(shù)的最終版。
<%
function buildSQLInsert( targetTable, omitFields)
iStr = "insert into " & targetTable & " "
vStr = "values ("
nStr = "("
' 在表單集合中循環(huán),并建立起SQL語句的組成部分
for each x in request.form
fieldName = uCase(x)
' 判斷字段是否被省略?
if inStr(uCase(omitFields),x) = 0 then
fieldData = replace(request.form(fieldName), _
"'", "''")
' 如果沒有數(shù)據(jù),就插入 NULL
if trim(fieldData) = "" then
fieldData = "NULL"
vStr = vStr & fieldData & ", "
nStr = nStr & fieldName & ", "
else
typeDelimPos = inStr(fieldName, "_")
if typeDelimPos = 0 then
' 是文本字段
' 建立字段名列表
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
end if
end if
next
' 把結(jié)尾的", " 從我們建立的字符串中去掉
vStr = left(vStr, len(vStr) - 2) & ")"
nStr = left(nStr, len(nStr) - 2) & ") "
' 把SQL語句整合起來
buildSQLInsert = iStr & nStr & vStr
end function
if trim(request("fName")&request("lname")&request("age")) <> "" then
response.write( buildSQLInsert("") & "<BR<")
response.write( buildSQLInsert("NUM_AGE") & "<BR>")
response.write( buildSQLInsert("lname,fname") & "<BR>")
response.write( buildSQLInsert("mycheckbox,fname") &
=<"<BR<")
else
%>
<html>
<body>
<form name=f method=post action="列表4.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="checkbox" value="Y" name="MyCheckBox"<Do you want this checked?<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<%
end if
%>