常用数据库操作的自定义函数

作者:袖梨 2022-06-30
<%
''一些asp开发过程中常用的数据操作,整理成了函数,提高开发效率
webdir="test/" ''路径,根据实际情况修改
function Conndb(ConnName)''连接数据库
set ConnName=server.createobject("adodb.connection")
ConnName.open "driver={Microsoft Access Driver (*.mdb)};dbq="&server.mappath(webdir & "inc/mydb.mdb")
'ConnName.open "Provider=SQLOLEDB;server=192.168.0.1;uid=sa;pwd=;database=test" ''使用sql server数据库时
end function
function close(ObjectName) '关闭对象
ObjectName.close
set ObjectName=nothing
end function
function CreateRs(RsName) ''创建数据集对象
set RsName=server.createobject("adodb.recordset")
end function
function getvalue(thisvalue)
getvalue=trim(request(thisvalue))
end function
'''以下函数形参定义解释
'''Rsname为定义数据集对象,DataName为表名,Fields为要获取的字段名称(以逗号隔开,空为所有)
'''Where为条件,order为排序方式
'''eg:call Readdb(rs,"tablename1","id,name,sex","sex='男' and id>10","id desc")
'''返回:记录集对象
sub ReadDb(RsName,DataName,Fields,Where,order) ''读取记录
Dim Source,recRsName,Mok
if trim(Fields)="" then Fields=" * "
Source="select "&Fields&" from "&DataName
if trim(where)<>"" then Source=Source&" where "&Where
if trim(order)<>"" then Source=Source&" order by "&order
set recRsName=Server.CreateObject("ADODB.Recordset")
recRsName.Open Source,conn,3,1
set RsName = recRsName
end sub
Function CountNumber(tablename,where)''获得表中满足条件的记录数
If len(trim(where))>0 Then
thiswhere=" where " & where
Else
thiswhere=""
End If
set rs=conn.execute("select count(*) as thisnumber from " & tablename & thiswhere)

相关文章

精彩推荐