0

目前我被一些经典的 asp 文件所困扰,但我想整合一些代码。我发现了这个并想征求意见。这是矫枉过正吗?一个健壮的连接类会有用吗?我打算尽我所能创建关注点分离,我正在研究模型。由于我不能通过扩展继承(我可以找到),我想我只是在每个模型文件中包含这个类:

http://www.sitepoint.com/forums/showthread.php?491770-Build-a-Database-Connections-Class-in-Classic-ASP

Class clsDatabaseConnections
    Private strConnection   ''# Connection string (change depending on what system we are using)
    Private objConn         ''# Connection object
    Private objComm         ''# Command Object
    Private objRS           ''# Recordset object

    Private Sub Class_Initialize()
        ''# What happens when the class is opened
        strConnection = "DRIVER={SQL Server}; ..........."

        Set objConn = Server.CreateObject("ADODB.Connection")
        objConn.ConnectionString = strConnection        
    End Sub

    Private Sub Class_Terminate()
        ''# What happens when the class is closed

        ''# Close connections
        If objConn.State <> 0 Then
            objConn.Close   
        End If
        Set objConn = Nothing
    End Sub         

    Public Sub SQLExecuteFromSQLString(ByRef strSQL)
        ''# Execute code and return nothing
        If objConn.State <> 0 Then
            objConn.Close
        End If  

        objConn.Execute strSQL      
    End Sub

    ''# This replicates the .NET ExecuteScalar
    Public Function ExecuteScalarFromSQLString(ByRef sSQL)
        ''# This is used when passing back single results. Replicating a .NET piece of functionality
        Dim objScalar
        Set objScalar = GetRecordSet(sSQL)

        If Not objScalar.EOF Then
            ExecuteScalar = objScalar(0)
        Else
            ''# Nothing returned
            ExecuteScalar = -1
        End If

        CloseRecordSet()
    End Function ''#ExecuteScalar

    Public Function GetRecordSetFromSQLString(ByRef strRS)
        If objConn.State <> 1 Then
            objConn.Open
        End If

        Set objRS = Server.CreateObject("ADODB.Recordset")
        objRS.Open strRS, objConn

        Set GetRecordSet = objRS
    End Function

    ''# Using SP code within class
    ''##########################################################################
    Public Sub CallSPNeedParams(ByRef strStoredProc)
        If objConn.State <> 1 Then
            objConn.Open
        End If

        If Not IsObject(objComm) Then
            Set objComm = Server.CreateObject("ADODB.Command") ''# This will be used for Stored Procedures
        End If

        With objComm
            .ActiveConnection = objConn
            .CommandText = strStoredProc
            .CommandType = adCmdStoredProc
        End With

        If Not IsObject(objRS) Then
            Set objRS = Server.CreateObject("ADODB.Recordset")
        End If

        Set objRS.ActiveConnection = objConn ''# Set connection
        Set objRS.Source = objComm  ''# Set source to use command object            
    End Sub

    Public Sub ApendParamsToRecordSet(ByRef Name, ByRef TypeParam, ByRef Direction, ByRef Size, ByRef Value)
        ''#Type adDate adDBDate, adVarChar, adChar, adBoolean
        If IsObject(objComm) Then
            objComm.Parameters.Append objComm.CreateParameter(Name, TypeParam, Direction, Size, Value)          
        End If
    End Sub

    Public Function GetRecordSetSPParams(ByRef strStoredProc)
        If strStoredProc = objComm.CommandText Then
            ''# This is being called for the right SP
            objRS.Open
            Set GetRecordSetSPParams = objRS

            ''# Need to clear out params from Command object
            Do While (objComm.Parameters.Count > 0)
                objComm.Parameters.Delete 0
            Loop

        End If
    End Function

    Public Sub CloseCommObject()
        If IsObject(objComm) Then
            Set objComm = Nothing
        End If
    End Sub
    ''##########################################################################

    Public Function ExecuteScalarSetSPParams(ByRef strStoredProc)
        ''# This is used when passing back single results. Replicating a .NET piece of functionality
        If strStoredProc = objComm.CommandText Then     
            objRS.Open
            If Not objRS.EOF Then
                ExecuteScalar = objRS(0)
            Else
                ''# Nothing returned
                ExecuteScalar = -1
            End If

            CloseRecordSet()
        End If
    End Function ''#ExecuteScalar               

    Public Sub ExecuteSPButNoRecordsReturned(ByRef strStoredProc)
        If strStoredProc = objComm.CommandText Then
            objComm.Execute
        End If
    End Sub ''#ExecuteSPButNoRecordsReturned()

    Public Sub CloseRecordSet() 
        If objRS.State <> 0 Then
            objRS.Close 
        End If

        Set objRS = Nothing
    End Sub

    Public Property Get ObjectConn()
        ObjectConn = objConn
    End Property    

    Public Property Let SwitchConnection(ByRef strConn)
        ''# Will allow user to change the connection from the default set up    
        strConnection = strConn
        Call SwitchConnection(strConnection)
    End Property

    Private Sub SwitchConnection(ByRef strConn)
        ''# Will allow user to change the connection from the default set up    
        strConnection = strConn
        If objConn.State <> adStateClosed Then
            objConn.ConnectionString = strConnection
        End If
    End Sub

End Class ''#clsDatabaseConnections
4

1 回答 1

1

简单的答案?这取决于。

如果您只有几个文件可以包含和使用这样的类,我会说创建这样的类并重构包含文件的努力不太可能。

如果您有一个大型库或一组库,您认为它们需要很长时间才能过时/替换,那就去吧。

过去,我为大型数据相关的 ASP 库创建了类似的(尽管不是那么健壮)类,将它们放置在文件夹层次结构中,这样它们就可以被许多应用程序使用。

除了简单之外,我看到这样做的最大好处是能够确保您的连接和其他资源被正确关闭/管理......您不想激怒 DBA ;?)

于 2012-08-14T18:57:41.027 回答