最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
自动生成插入,修改的SQL
时间:2022-06-30 10:29:48 编辑:袖梨 来源:一聚教程网
数据库设计的小网站,表单多而操作简单,一般就只有插入删除修改等操作。每次都要重复写插入修改的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={sql server};server=sundy;database=test;uid=sa;pwd=;")
Set conn = Server.CreateObject("ADODB.Connection")
'下面以Access数据库为例
dbconn = "driver={Microsoft Access Driver (*.mdb)};dbq=" & SERVER.MapPath("data/db.mdb")
conn.open dbconn
'表的主键字段名
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
If INSTR(",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
是不是觉得很麻烦呢,我是这么觉得,所以,自己写了一个自动产生插入,修改的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={sql server};server=sundy;database=test;uid=sa;pwd=;")
Set conn = Server.CreateObject("ADODB.Connection")
'下面以Access数据库为例
dbconn = "driver={Microsoft Access Driver (*.mdb)};dbq=" & SERVER.MapPath("data/db.mdb")
conn.open dbconn
'表的主键字段名
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
If INSTR(",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