資料|資料庫|語句 <%
'在指定的資料庫上運行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
%>