ASP数据库操作常用函数集,经典的东西收藏备用。
代码来源于Mir ZhouYang。
dbinit() 连接数据库
opendb(asqlstr,rs_temp) 打开记录集的函数
selectdb(awherestr,dbname,rs_temp) 打开表的函数
deletedb(awherestr,dbname) 数据库删除记录的函数
updatedb(aarr,awherestr,dbname) 数据库修改记录的函数
insertdbex(aarr,dbname,rs_temp) 数据库增加记录的函数
insertdbrs(aarr,dbname) 数据库增加记录(insert into)返回当前记录id
insertdb(aarr,dbname) 数据库插入记录的函数
dbclose() 页面结束关闭数据库
ASP/Visual Basic代码
- <%
- '=============================================
- 'dbinit() 连接数据库
- 'opendb(asqlstr,rs_temp) 打开记录集的函数
- 'selectdb(awherestr,dbname,rs_temp) 打开表的函数
- 'deletedb(awherestr,dbname) 数据库删除记录的函数
- 'updatedb(aarr,awherestr,dbname) 数据库修改记录的函数
- 'insertdbex(aarr,dbname,rs_temp) 数据库增加记录的函数
- 'insertdbrs(aarr,dbname) 数据库增加记录(insert into)返回当前记录id
- 'insertdb(aarr,dbname) 数据库插入记录的函数
- 'dbclose() 页面结束关闭数据库
- '========================================
- dim Conn,rs_Event
- Set rs_Event = Server.CreateObject("ADODB.RecordSet")
- function dbinit()
- if conn <> null then
- exit function
- end if
- Set conn = Server.CreateObject("ADODB.Connection")
- conn.ConnectionTimeout = 10
- conn.CommandTimeout = 30
- conn.CursorLocation = 3
- conn.ConnectionString = "driver={SQL Server};server="+database_servername+";uid="+database_loginname+";pwd="+database_password+";database="+database_name
- on Error Resume Next
- conn.Open
- if err<>0 then
- response.write "请验查数据库服务器的连接信息是否正确。"
- response.end
- end if
- end function
- function opendb(asqlstr,rs_temp)
- '打开记录集的函数
- 'asqlstr 是输入sql语句
- 'rs_temp 是输入记录集变量
- 'on Error Resume Next
- Set rs_temp = Server.CreateObject("ADODB.RecordSet")
- if rs_temp.state = adStateOpen then
- rs_temp.close
- end if
- ' Response.Write asqlstr
- set rs_temp=server.createobject("adodb.recordset")
- rs_temp.CursorLocation=adUseClient
- rs_temp.open asqlstr, conn,adOpnestatic,adLockReadOnly,adCmdText
- end function
- function selectdb(awherestr,dbname,rs_temp)
- '打开表的函数
- 'awherestr 是sql中条件很语句
- 'dbname 是数据表名
- 'rs_temp 是记录集变量
- 'on Error Resume Next
- selectdb = 1
- if rs_temp.state = adStateOpen then
- rs_temp.close
- end if
- rs_temp.CursorLocation=adUseClient
- 'response.write "Select * from "&dbname&awherestr
- rs_temp.open "Select * from "&dbname&awherestr, conn,adOpnestatic,adLockReadOnly,adCmdText
- if Conn.Errors.Count > 0 then
- selectdb = 0
- end if
- end function
- function selectdbtop(awherestr,dbname,rs_temp)
- '打开表的函数
- 'awherestr 是sql中条件很语句
- 'dbname 是数据表名
- 'rs_temp 是记录集变量
- 'on Error Resume Next
- selectdb = 1
- if rs_temp.state = adStateOpen then
- rs_temp.close
- end if
- rs_temp.CursorLocation=adUseClient
- 'response.write "Select * from "&dbname&awherestr
- rs_temp.open "Select * from "&dbname&awherestr, conn,adOpnestatic,adLockReadOnly,adCmdText
- if Conn.Errors.Count > 0 then
- selectdb = 0
- end if
- end function
- function deletedb(awherestr,dbname)
- '数据库删除记录的函数
- 'awherestr 是sql中条件很语句
- 'dbnaem 是数据库名称
- 'on Error Resume Next
- deletedb = 1
- Conn.Execute "delete from "&dbname&awherestr
- if Conn.Errors.Count > 0 then
- deletedb = 0
- end if
- end function
- function updatedb(aarr,awherestr,dbname)
- '数据库修改记录的函数
- 'aarr 修改数组
- 'awherestr 是sql中条件很语句
- 'dbnaem 是数据库名称
- 'on Error Resume Next
- dim i
- updatedb = 1
- t_values = ""
- for i=0 to ubound(aarr) - 1
- t_values=t_values&aarr(i,0)"='"&replace(aarr(i,1),"'","''")"',"
- next
- t_values=left(t_values,len(t_values) -1)
- Conn.Execute "update "&dbname" set "&t_values&awherestr
- if Conn.Errors.Count > 0 then
- updatedb = 0
- end if
- end function
- function insertdbex(aarr,dbname,rs_temp)
- 'on Error Resume Next
- dim i
- insertdbex = 1
- if rs_temp.state = adStateOpen then
- rs_temp.close
- end if
- rs_temp.ActiveConnection = conn
- rs_temp.CursorType = adOpenKeyset
- rs_temp.LockType = adLockOptimistic
- rs_temp.source = dbname
- rs_temp.Open
- rs_temp.AddNew
- for i=0 to ubound(aarr) - 1
- rs_temp(aarr(i,0)) = aarr(i,1)
- next
- rs_temp.update
- if Err.number <> 0 then
- response.write err.description
- response.end
- insertdbex = 0
- end if
- end function
- function insertdb(aarr,dbname)
- '数据库插入记录的函数
- 'dbnaem 是数据库名称
- 'on Error Resume Next
- dim i
- err.clear
- insertdb = 1
- t_values = ""
- t_fields = ""
- for i=0 to ubound(aarr) - 1
- t_fields = t_fields&aarr(i,0)","
- t_values=t_values"'"&replace(aarr(i,1),"'","''")"',"
- next
- t_values=left(t_values,len(t_values) -1)
- t_fields=left(t_fields,len(t_fields) -1)
- Conn.Execute "insert into "&dbname" ("&t_fields") values ("&t_values")"
- if (Conn.Errors.Count > 0) or (err.number <> 0) then
- insertdb = 0
- end if
- end function
- function insertdbrs(aarr,dbname)
- '数据库插入记录的函数
- 'aarr 增加数组
- 'dbnaem 是数据库名称
- 'on Error Resume Next
- dim i
- err.clear
- insertdbrs = 1
- t_values = ""
- t_fields = ""
- for i=0 to ubound(aarr) - 1
- t_fields = t_fields&aarr(i,0)","
- t_values=t_values"'"&replace(aarr(i,1),"'","''")"',"
- next
- t_values=left(t_values,len(t_values) -1)
- t_fields=left(t_fields,len(t_fields) -1)
- set returnrs=Conn.Execute("SET NOCOUNT ON;insert into "&dbname" ("&t_fields") values ("&t_values");SELECT @@IDENTITY")
- if (Conn.Errors.Count > 0) or (err.number <> 0) then
- insertdbrs = 0
- end if
- insertdbrs=returnrs(0)
- returnrs.close
- end function
- '页面结束关闭数据库
- function dbclose()
- Conn.Close
- end function
- %>