Employee: EMPLID char (5) not null, Name char (10) not null, Gender char (1) not null, Score int not null 此表存儲員工信息和考試成績,為簡單起見,這里只包含工號,姓名和性別三項(xiàng),并且只有一門考試,EMPLID為主鍵。
2。建立動態(tài)鏈接庫 啟動VB(這里以VB為例,你可以用你喜歡的任何支持ActiveX接口的開發(fā)工具開發(fā)),新建一工程,工程類型為ActiveX DLL。在工程中新建一個類,取名為Employee。你可以Class Builder可視化的向類中填加屬性和方法,也可以直接手工編輯。首先填加EMPLID屬性如下: PRivate msEMPLID as string Property Let EMPLID(sEMPLID as string) msEMPLID=sEMPLID End Property Property Get EMPLID() as string EMPLID=msEMPLID End Property
Public Sub Create(EMPLID as string) dim conn as new Connection dim rs as new Recordset dim sql as string "Suppose that you create a DSN in the control panel, the connectionstring property "can also be dsn-less string conn.ConnectionString="dsn=dsnname;uid=username;passWord=pwd" conn.open sql="select * from Employee where EMPLID="" & EMPLID & """ with rs .open sql,conn,1,3 if .eof and .bof then exit sub else msEMPLID=trim(.Fields("EMPLID")) msName=trim(.Fields("Name")) msGender=trim(.Fields("Gender")) msScore=.Fields("Score") end if .close end with set rs=nothing conn.close set conn=nothing End Sub 這里根據(jù)EMPLID創(chuàng)建Employee對象,注意數(shù)據(jù)庫中的值是賦給三個私有變量,而不是直接賦值給屬性,如果你單步調(diào)試就會發(fā)現(xiàn),給msEMPLID賦值會調(diào)用Property Let EMPLID,也就是給屬性賦值。 下面我們再創(chuàng)建一個類Employees,并填加如下方法: private colQualifiedList as new Collection private mnCurrentIndex as integer Public Sub GetQualifiedList() dim conn as new Connection dim rs as new Recordset dim sql as string "Suppose that you create a DSN in the control panel, the connectionstring property "can also be dsn-less string conn.ConnectionString="dsn=dsnname;uid=username;password=pwd" conn.open sql="select EMPLID from Employee where Score>=60 order by Score desc" with rs .open sql,conn,1,3 if .eof and .bof then exit sub else do while not .eof dim oEmployee as new Employee oEmployee.Create trim(.Fields("EMPLID")) colQualifiedList.Add oEmployee set oEmployee=nothing loop end if .close end with set rs=nothing conn.close set conn=nothing End Sub 首先請注意VB中創(chuàng)建類實(shí)例的語法dim oEmployee as new Employee,后面會看到,在ASP中創(chuàng)建類實(shí)例的語法是不同的。這個方法檢索成績大于等于60的員工工號,并據(jù)此創(chuàng)建一個Employee對象,再將此對象加入私有的集合對象中。下面兩個函數(shù)遍歷集合中的元素: Public Function GetFirst() as Employee if colQualifiedList.count>0 then mnCurrentIndex=1 set GetFirst=colQualifiedList.Item(1) else set GetFirst=nothing end if End Function Public Function GetNext() as Employee mnCurrentIndex=mnCurrentIndex+1 if mnCurrentIndex>colQualifiedList.count then set GetNext=nothing else set GetNext=colQualifiedList.Item(mnCurrentIndex) End if End Function 也許你會說,為何不把集合聲明Public,這樣在ASP中不是可以直接引用嗎?確實(shí),這樣也行得通,編程實(shí)現(xiàn)起來也更簡單些,但是,這樣做破壞了封裝性原則。因?yàn)閿?shù)據(jù)以何格式存儲完全是商業(yè)邏輯層的事,與用戶界面層無關(guān),假設(shè)有一天你因?yàn)槊糠N原因放棄了用集合來存儲數(shù)據(jù)的設(shè)計,而改用數(shù)組或記錄集(Recordset)來存儲,那你只需要修改GetFirst和GetNext兩個函數(shù),用戶界面層完全無需修改。 至此類文件創(chuàng)建完畢,將工程文件存為 test.vbp,選File菜單下的Make test.dll選項(xiàng)將其編譯。 3.注冊動態(tài)鏈接庫 啟動Web Server 上的Microsoft Transaction Server (Start--Windows NT Optionpack4--Internet Information Server--Internet Service Manager),展開Microsoft Transaction Server--Computer--My Computer--Package Installed,點(diǎn)鼠標(biāo)右鍵選New--Package--Create Empty Package,輸入包名Test(這里Test是任選的名字,不一定要與DLL同名),OK-Interactive User-the current Logon user--Finish。雙擊Test--Component,右鍵選Component-New-Component-Install New component(s)-- Add File,選擇你剛編譯好的DLL文件,MTS會發(fā)現(xiàn)DLL中有兩個類Employee和Employees。至此DLL注冊完畢。
4.編寫ASP程序 <HTML><Body> <p>Qualified Employee List</p> <table border=1 cellspacing=0 cellpadding=0> <tr> <td>Employee ID</td> <td>Name</td> <td>Gender</td> <td>Score</td> </tr> <% set oEmployees=server.createobject("Test.Employees") oEmployees.GetQualifiedList set oEmployee=oEmployees.GetFirst() do while not oEmployee is nothing %> <tr> <td><%=oEmployee.EMPLID%></td> <td><%=oEmployee.Name%></td> <td><%=oEmployee.Gender%></td> <td><%=oEmployee.Score%></td> </tr> <% set oEmployee=oEmployees.GetNext() loop %> </table> </body></html> 注意在ASP中創(chuàng)建類實(shí)例的語法set oEmployees=server.createobject("Test.Employees"),其中Test是DLL的名字,Employees是類的名字; 當(dāng)然,如果一個函數(shù)的返回值是一個對象,類似set oEmployee=oEmployees.GetFirst()這樣的語法也是可以的。 至此,一個完整的三層結(jié)構(gòu)的應(yīng)用程序已經(jīng)完成了,讓我們看以下,如果把"合格"的定義改為:只有成績進(jìn)入前100名才算合格,程序需要做那些修改。事實(shí)上,如果你的數(shù)據(jù)庫系統(tǒng)是SQL Server,你只需把SQL語句改為: sql="select top 100 EMPLID from Employee order by Score desc" 就已經(jīng)可以了,即使為了跨數(shù)據(jù)庫系統(tǒng)的兼容性,我們也只需要對GetQualifiedList做如下修改:
...... sql="select EMPLID from Employee order by Score desc" with rs .open sql,conn,1,3 if .eof and .bof then exit sub else i=1 do while (not .eof) and (i<=100) dim oEmployee as new Employee oEmployee.Create trim(.Fields("EMPLID")) colQualifiedList.Add oEmployee set oEmployee=nothing i=i+1 loop end if .close end with ...
Public Function Fun1(p1 as string,p2 as integer) as integer End Function 在ASP程序中應(yīng)如下調(diào)用: <% p1=obj.property1 " Property1 is a string property p2=obj.property2 "Property2 is an integer property a=obj.Fun1(cstr(p1),cint(p2)) a=obj.Fun1("aaa",10) " constant parameter need not be changed %>
而下面的兩種寫法是錯誤的:
<% p1=obj.property1 " Property1 is a string property p2=obj.property2 "Property2 is an integer property a=obj.Fun1(p1,p2) " incorrect,p1 and p2 are variant variables p1=cstr(p1) p2=cint(p2) a=obj.Fun1(p1,p2) " still incorrect %> 這里第二種寫法仍然是錯誤的,即使經(jīng)過了類型轉(zhuǎn)換,p1和p2仍然是Variant變量。在VBScript中,數(shù)據(jù)類型和類型轉(zhuǎn)換函數(shù)只在表達(dá)式運(yùn)算中起作用,變量只有Variant一種類型。