sundy原創 2004-11-02
資料庫設計的小**,表單多而操作簡單,一般就只有插入刪除修改等操作。每次都要重複寫插入修改的sql,
是不是覺得很麻煩呢,我是這麼覺得,所以,自己寫了乙個自動產生插入,修改的sql,就方便多了,一下是asp**,
對access,sql server都適用。如果對你有幫助,可以拿去用用,還可以改進。
<%
dim tablename,strsql,rsfields,fieldscount,totalcount
dim insertsql,ifields,idatafields,updatesql,deletesql
dim keyfieldname,funquote
dim conn
dim dbpath
'sql server 連線字串
'conn.open("driver=;server=sundy;database=test;uid=sa;pwd=;")
'表的主鍵欄位名
keyfieldname = request.form("keyfield")
'用於過濾提交表單中的「'「的函式名
funquote = request.form("funquote")
'表名tablename = request("tablename")
if request.form("subtable")<> ""
then
strsql = "select top 1 * from " & tablename
set rsfields = server.createobject("adodb.recordset")
rsfields.open strsql,conn,1,1
fieldscount = rsfields.fields.count
insertsql = ""
"insert into " & tablename & "("
" & _" & vbcrlf
updatesql = ""
"update " & tablename & " set "
" & _" & vbcrlf
for i = 0 to fieldscount - 1
ifinstr(",3,202,203,","," & rsfields.fields(i).type & ",") > 0 and
ucase(rsfields.fields(i).name) <> ucase(keyfieldname) then
ifields = ifields & ""
"" & rsfields.fields(i).name
select
case rsfields.fields(i).type
case 3 'int
idatafields = idatafields & ""
""" & request.form("
"" & rsfields.fields(i).name & ""
") & "
""updatesql = updatesql & ""
"" & rsfields.fields(i).name & "="
" & request.form("
"" & rsfields.fields(i).name & ""
") & "
""case 202 'char
idatafields = idatafields & ""
"'"" & " & funquote & "(request.form("
"" & rsfields.fields(i).name & ""
")) & "
"'"updatesql = updatesql & ""
"" & rsfields.fields(i).name & "='"
" & " & funquote & "(request.form("
"" & rsfields.fields(i).name & ""
")) & "
"'"case 203 'text
idatafields = idatafields & ""
"'"" & " & funquote & "(request.form("
"" & rsfields.fields(i).name & ""
")) & "
"'"updatesql = updatesql & ""
"" & rsfields.fields(i).name & "='"
" & " & funquote & "(request.form("
"" & rsfields.fields(i).name & ""
")) & "
"'"end
select
ifields = ifields & ", "
" & _" & vbcrlf
idatafields = idatafields & ","
" & _ " & vbcrlf
updatesql = updatesql & ","
" & _ " & vbcrlf
endif
next
ifields = mid(ifields,1,len(ifields) - 9)
idatafields = mid(idatafields,1,len(idatafields) - 9)
insertsql = insertsql & ifields & ") values ("
" & _" & vbcrlf & idatafields & ")"
""updatesql = mid(updatesql,1,len(updatesql) - 9) & ""
" & _ " & vbcrlf & ""
" where " & keyfieldname & "="
" & request.querystring("
"" & keyfieldname & ""
")"end
ifconn.close()
set conn = nothing
%>