<!-- 蛙蛙推薦:VBS的數(shù)據(jù)庫操作類,這個類實現(xiàn)了平時主要的數(shù)據(jù)庫操作,因為我們天天都在和數(shù)據(jù)庫打交道,有時候用這樣一個類也挺方便的,但是不知道實用性怎么樣,因為在性能和錯誤處理方面沒有做很多考慮,所以不知道它的通用性如何. 如果誰能再給這個類加上動態(tài)的錯誤處理的或者優(yōu)化一下性能,就更完美了.PS:做這個類也是為了練習(xí)一下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 %>
|