Username: Password:

在线建库-ASP教程,数据库相关
来源:作者: 发布时间:2007-12-26 02:11:15

<%
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="建立数据库失败。
"+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
%>

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅