新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > Web开发 > Asp教程 > 正文:在指定的数据库上运行SQL语句的类

在指定的数据库上运行SQL语句的类

新客网 XKER.COM 2004-03-18 来源: 收藏本文
<%
'在指定的数据库上运行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
%>
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐