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

<%
on error resume next
class createdb
'
'
'建立一个数据库
'用法:
'dim cdb
'set cdb=new createdb
'cdb.setdbname=数据库名
'if cdb.ifok then response.end 数据库已经存在
'cdb.run
'检查是否运行成功
'if cdb.ifok then
'response.write cdb.errs
'end if

private dbname '数据库名字
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="在线建库"
end sub

'设置数据库名
property let setdbname(byval dbn)

dbname=dbn
'检查数据库是否已经存在
ifexistdb dbn

end property

public sub run()

'
class_initialize
'检查数据库名是否为空
if isnull(dbname) or isempty(dbname) or cstr(dbname)="" then
errstr="建立数据库失败,数据库名不能为空"
ifsure=true
exit sub
end if

'这句不能放在ifexistdb里,也不能随后执行,因为找不到数据库后自动退出这个类,
'可能是太严重的错误吧,所以只能在另一个地方清理错误码了
err.clear

dim objcreate '保存ADOX.CATALOG对象

set objcreate=Server.CreateObject("adox.catalog")
if err.number<>0 then
errstr="建立adox.catalog对象失败,请检查你的用户权限。"+err.description
set objcreate=nothing
ifsure=true
exit sub
end if

'建立数据库
objcreate.create("data source="+server.mappath(dbname)+";provider=microsoft.jet.oledb.4.0")
if err.number<>0 then
errstr="建立数据库失败。<br>"+err.description
ifsure=true
set objcreate=nothing
exit sub
end if
'如果没有出错,设置成功标志
ifsure=false

end sub



private sub ifexistdb(byval dbn)
'还原类状态
ifsure=false
'如果数据库存在,就设为true,因为如果不存在的话就不能继续执行这个类
'检查数据库是否已经存在
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
errstr="数据库已经存在"
ifsure=true
conn.close
set conn=nothing
end if


end sub

end class
%>