日期:2012-06-27  浏览次数:20859 次

<%
'在指定的数据库上运行SQL语句的类
'使用方法:
'dim runs
'set runs=new runsql
'runs.setdbn=数据库名
'if runs.ifok then
' response.write runs.errs
' response.end
'end if
'runs.setsql=sql
'runs.run
'if runs.ifok then
' response.write runs.errs
' response.end
'else
' response.write "执行成功"
'end if



on error resume next
class runsql

private dbname '数据库名

private sql '要执行的SQL语句
private ifsure '用来保存是否成功的标志,如果成功值为false,失败为true,初值为true
private errstr '保存说明错误的文字

'获取ifsure值
property get ifok()
ifok=ifsure
end property

'获取errstr值
property get errs()
errs=errstr
end property

'
private sub class_initialize()
'设置ifsure,errstr的初值
ifsure=true
errstr="对指点数据库执行SQL语句"
end sub


'给dbname赋值
property let setdbn(dbn)
dbname=dbn
ifexistdb dbn
end property

'给SQL赋值
property let setsql(s)
sql=s
end property

'执行操作
public sub run()
'还原类状态
class_initialize

'检查参数是否已经填写完整
if isnull(dbname) or isempty(dbname) or cstr(dbname)="" then
errstr="dbname不能为空"
exit sub
end if

if isnull(sql) or isempty(sql) or cstr(sql)="" then
errstr="sql不能为空"
exit sub
end if


dim conn '连接数据库对象
set conn=Server.CreateObject("adodb.connection")
if err.number<>0 then
errstr="建立adodb.connection对像失败."
set objcreate=nothing
exit sub
end if

errstr="不能连接数据库"

'连接数据库
conn.connectionstring="provider=microsoft.jet.oledb.4.0;data source="+server.mappath(dbname)
conn.open



errstr="执行SQL语句失败"
'执行SQL语句

conn.execute(sql)

'如果没出错 设置成功标志
if err.number=0 then
ifsure=false
end if

end sub



private sub ifexistdb(byval dbn)
'还原类状态
class_initialize
'如果数据库存在,就设为true,因为如果不存在的话就不能继续执行这个类
'检查数据库是否已经存在

errstr="数据库不存在"

dim conn
set conn=server.createobject("adodb.connection")
conn.connectionstring="provider=microsoft.jet.oledb.4.0;data source="+server.mappath(dbn)
conn.open

if err.number=0 then
ifsure=false
end if

end sub
end class
%>