2012-08-14 51 views
0

我暫時停留在一些經典的asp文件中,但我想合併一些代碼。我發現這一點,並希望得到意見。這是否過分矯枉過正?健壯的連接類會有用嗎?我試圖儘可能創造一種分離的擔憂,我正在研究這個模型。既然不能繼承的延伸(即我能找到),我想我只是包含這個類中的每個模型文件:是傳統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 
+0

http://www.shiningstar.net/articles/articles/database/datafunctions.asp?ID=AW – pee2pee 2014-10-09 10:46:21

回答

1

簡單的答案?這取決於。

如果你只有幾個文件可以包含和使用這樣的類,那麼我認爲創建這樣一個類並重構包含文件是不值得的。

如果您有大型圖書館或一系列您認爲需要過時或更換的大量圖書館,

在過去,我爲大型數據相關ASP庫創建了類似的(雖然不是很強大)類,將它們放置在文件夾層次結構中,以便它們可以被衆多應用程序使用。

除了簡單之外,我所看到的最大好處是能夠確保您的連接和其他資源被正確關閉/管理......您不想憤怒DBA;)?

相關問題