日期:2014-04-26  浏览次数:20318 次

<!--
蛙蛙推荐:VBS的数据库操作类,这个类实现了平时主要的数据库操作,因为我们天天都在和数据库打交道,有时候用这样一个类也挺方便的,但是不知道实用性怎么样,因为在性能和错误处理方面没有做很多考虑,所以不知道它的通用性如何.
如果谁能再给这个类加上动态的错误处理的或者优化一下性能,就更完美了.PS:做这个类也是为了练习一下VBS类的使用.
-->
<%
class classado
Private conn_c
Private rs_c
Public strconn
Private Sub Class_Initialize'定义类的初始化事件

End Sub

Private Sub Class_Terminate ' 设置 Terminate 事件。 '定义类的清空事件
rs_c.close
set rs_c=nothing
conn_c.close
set conn_c=nothing
End Sub

Public Function opendb() '打开数据库
if isempty(strconn) then
response.Write("没有设置数据库连接字符串")
response.end
end if
if isempty(conn_c) then
Set conn_c=Server.CreateObject("ADODB.Connection")
conn_c.open strconn
else
response.Write("数据库已经打开了")
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) '执行一条sql语句,用来插入,更新,删除记录
if isempty(conn_c) then
opendb()
end if
conn_c.execute(strsql)
End Function
end class
%>
<%
'on error resume next '调试程序的时候请把此句去掉
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>