2010-01-14 37 views
1

我想要在很多页面上使用的对象包含自己的连接和记录集变量,以便它们不需要在每个想要直接访问记录集的页面上声明,而不需要进行交互与通常处理这个对象的功能。VBScript类/对象问题

但是,记录集显然不成为对象。

<!-- 
METADATA 
TYPE="TypeLib" 
NAME="Microsoft ActiveX Data Objects 2.5 Library" 
UUID="{00000205-0000-0010-8000-00AA006D2EA4}" 
--> 

而且

<% 
Option Explicit 
Class cls 
Public conn 
Public rs 

Public Sub OpenRS(ByRef conn, ByRef rs, ByRef sql, ByRef Mode, 
        ByRef CursorType, ByRef LockType) 
    Set conn = Server.CreateObject("ADODB.Connection") 
    conn.Provider = "Microsoft.Jet.OLEDB.4.0" 
    conn.Mode = Mode 
    conn.Open = Server.MapPath(".") & "\mb\testdb.mdb" 
    Set rs = Server.CreateObject("ADODB.Recordset") 
    rs.CursorType = CursorType 
    rs.LockType = LockType 
    rs.ActiveConnection = conn 
    rs.Open sql 
End Sub 
Public Sub CloseRS(ByRef conn, ByRef rs) 
    If VarType(rs) = vbObject Then rs.Close 
    Set rs = Nothing 
    If VarType(conn) = vbObject Then conn.Close 
    Set conn = Nothing 
End Sub 
Private Sub Class_Initialize() 
    Set conn = Nothing 
    Set rs = Nothing 
End Sub 
End Class 

Dim a: Set a = New cls 
a.OpenRS a.conn,a.rs, "SELECT * FROM emp", 
     adModeRead, adOpenForwardOnly, adLockPessimistic 
Response.Write(a.rs.EOF) 
%> 
+0

OpenRS过程创建连接和记录集,那么为什么你将它们作为参数传递?尝试不传递连接或记录集,只传递sql,cursortype和locktype。 – Tester101 2010-01-14 14:52:16

回答

0

尽量不传递rs参数:

Public Sub OpenRS(ByRef conn, ByRef sql, ByRef Mode, 
        ByRef CursorType, ByRef LockType) 
    Set conn = Server.CreateObject("ADODB.Connection") 
    conn.Provider = "Microsoft.Jet.OLEDB.4.0" 
    conn.Mode = Mode 
    conn.Open = Server.MapPath(".") & "\mb\testdb.mdb" 
    '' // will set public property, not that old parameter 
    Set rs = Server.CreateObject("ADODB.Recordset") 
    rs.CursorType = CursorType 
    rs.LockType = LockType 
    rs.ActiveConnection = conn 
    rs.Open sql 
End Sub 

像这样

a.OpenRS a.conn, "SELECT * FROM emp", 
     adModeRead, adOpenForwardOnly, adLockPessimistic 
0

这对我的作品的使用。注意在调用OpenRS时,我没有传递连接或记录集。

Class cls 
    Public conn 
    Public rs 

    Public Sub OpenRS(ByRef sql, ByRef Mode, ByRef CursorType, ByRef LockType) 
     Set conn = Server.CreateObject("ADODB.Connection") 
     conn.Provider = "Microsoft.Jet.OLEDB.4.0" 
     conn.Mode = Mode 
     conn.Open = Server.MapPath(".") & "\mb\testdb.mdb" 
     Set rs = Server.CreateObject("ADODB.Recordset") 
     rs.CursorType = CursorType 
     rs.LockType = LockType 
     rs.ActiveConnection = conn 
     rs.Open sql 
    End Sub 

    Public Sub CloseRS(ByRef conn, ByRef rs) 
     If VarType(rs) = vbObject Then rs.Close 
     Set rs = Nothing 
     If VarType(conn) = vbObject Then conn.Close 
     Set conn = Nothing 
    End Sub 

    Private Sub Class_Initialize() 
     Set conn = Nothing 
     Set rs = Nothing 
    End Sub 
End Class 


Set a = New cls 
a.OpenRS "SELECT * FROM emp", adModeRead, adOpenForwardOnly, adLockPessimistic 

您不必传递它自己的属性的对象,它已经有权访问它们。

2

我的版本: -

Option Explicit 
Class RSManager 
    Private conn 
    Private rs 

    Public Property Get RecordSet() 
    Set RecordSet = rs 
    End Property 

    Public Function OpenRS(ByVal sql, ByVal Mode, 
        ByVal CursorType, ByVal LockType) 
    Set conn = Server.CreateObject("ADODB.Connection") 
    conn.Provider = "Microsoft.Jet.OLEDB.4.0" 
    conn.Mode = Mode 
    conn.Open = Server.MapPath("/App_Data") & "\mb\testdb.mdb" 

    Set rs = Server.CreateObject("ADODB.Recordset") 
    rs.CursorType = CursorType 
    rs.LockType = LockType 
    rs.ActiveConnection = conn 
    rs.Open sql 
    Set OpenRS = rs 
    End Sub 

    Public Sub CloseRS() 
    If Not rs Is Nothing Then 
     If rs.State = adStateOpen Then rs.Close 
     Set rs = Nothing 
    End If 
    If Not conn Is Nothing Then 
     If conn.State = adStateOpen Then conn.Close 
     Set conn = Nothing 
    End If 
    End Sub 

    Private Sub Class_Initialize() 
    Set conn = Nothing 
    Set rs = Nothing 
    End Sub 

    Private Sub Class_Terminate() 
    CloseRS 
    End Sub 
End Class 

Dim RSMEmp: Set RSMEmp = New RSManager 
Dim rs : Set rs = RSMEMp.OpenRS "SELECT * FROM emp", 
    adModeRead, adOpenForwardOnly, adLockPessimistic 

Response.Write(rs.EOF) 

注: -

  • 连接现在是私人和访问内部记录是只读的。
  • OpenRS不依赖于呼叫者在其自身的成员通过用于分配
  • OpenRS返回便于学习记录
  • OpenRS使用绝对路径中的MapPath,从而消除了与特定的文件夹中包括的耦合。
  • CloseRS现在腰带和大括号
  • 终止事件添加,以确保记录集和连接关闭,如果消费代码未能调用CloseRS。

编辑

也许我应该详细说明“闭门现在腰带和背带”比其更实际一点。原始似乎试图确保Close不必在不必要时或当变量设置为无。不幸的是,设置为Nothing的变量的VarType仍然是vbObject,因此在调用时原始代码已被调用,或者从未调用过OpenRS会导致错误。

+0

+1为单挑战。 – Tester101 2010-01-14 16:02:04