VBS的數(shù)據(jù)庫操作類,
2024-07-21 02:15:39
供稿:網(wǎng)友
本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。<!--
蛙蛙推薦:vbs的數(shù)據(jù)庫操作類,這個類實現(xiàn)了平時主要的數(shù)據(jù)庫操作,因為我們天天都在和數(shù)據(jù)庫打交道,有時候用這樣一個類也挺方便的,但是不知道實用性怎么樣,因為在性能和錯誤處理方面沒有做很多考慮,所以不知道它的通用性如何.
如果誰能再給這個類加上動態(tài)的錯誤處理的或者優(yōu)化一下性能,就更完美了.ps:做這個類也是為了練習一下vbs類的使用.
-->
<%
class classado
private conn_c
private rs_c
public strconn
private sub class_initialize'定義類的初始化事件
end sub
private sub class_terminate ' 設(shè)置 terminate 事件。 '定義類的清空事件
rs_c.close
set rs_c=nothing
conn_c.close
set conn_c=nothing
end sub
public function opendb() '打開數(shù)據(jù)庫
if isempty(strconn) then
response.write("沒有設(shè)置數(shù)據(jù)庫連接字符串")
response.end
end if
if isempty(conn_c) then
set conn_c=server.createobject("adodb.connection")
conn_c.open strconn
else
response.write("數(shù)據(jù)庫已經(jīng)打開了")
response.end
end if
end function
public function getrs(byval strsql) '獲取記錄集
if isempty(conn_c) then
opendb()
end if
set rs_c=server.createobject("adodb.recordset")
rs_c.open strsql,conn_c,1,1
set getrs=rs_c
end function
public function exesql(byval strsql) '執(zhí)行一條sql語句,用來插入,更新,刪除記錄
if isempty(conn_c) then
opendb()
end if
conn_c.execute(strsql)
end function
end class
%>
<%
'on error resume next '調(diào)試程序的時候請把此句去掉
strconn="driver={sql server};server=192.168.0.110;database=northwind;uid=sa;pwd=sa;"
set c=new classado
c.strconn=strconn
c.opendb()
strsql="select employeeid,titleofcourtesy + '' + lastname + '' + firstname as fullname from employees"
arr_wawa=c.getrs(strsql).getrows()
set rs=c.getrs(strsql)
%>
<table width="100%" border="0" cellspacing="1">
<%
if not rs.eof then
for i=0 to rs.fields.count-1
response.write rs.fields(i).name&" "
next
response.write "<br>"
do while not rs.eof
for i=0 to rs.fields.count-1
response.write rs.fields(i).value&" "
next
response.write "<br>"
rs.movenext
loop
end if
%>
<hr>
<table width="100%" border="0" cellspacing="1">
<% if not isempty(arr_wawa) then %>
<% for i=0 to ubound(arr_wawa,2) %>
<tr>
<% for j=0 to ubound(arr_wawa,1) %>
<td><%= arr_wawa(j,i) %></td>
<% next %>
</tr>
<% next %>
<% else %>
<tr>
<td>沒有記錄</td>
</tr>
<% end if %>
</table>