2012-08-14 2 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

답변

1

간단한 답 : 나는에 의해 상속 할 수 없기 때문에 (내가 찾을 수), 난 그냥 각 모델 파일에이 클래스를 포함하는 줄 알았는데 연장? 그것은 달려있다.

이러한 클래스를 포함하고 사용하는 파일이 몇 개 밖에없는 경우 이러한 클래스를 만들고 포함 파일을 리팩터링하는 것은 그만한 가치가 있다고 말할 수 있습니다.

노후화/대체에 상당한 시간이 있다고 생각되는 대형 라이브러리 또는 라이브러리가있는 경우 이동하십시오.

이전에는 대형 데이터 종속 ASP 라이브러리에 대해 유사한 (비록 강력하지는 않지만) 유사한 클래스를 만들어서 수많은 응용 프로그램에서 사용할 수 있도록 폴더 계층에 배치했습니다.

단순함 이외에도 연결 및 기타 리소스를 올바르게 닫고 관리 할 수있는 능력이 있습니다 ... DBA에 분노를 느끼지 않으려 고합니다.)

관련 문제