Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

128 lignes
4.5KB

  1. <%
  2. '------------------------------------------------------------------------------
  3. ' DatabaseConnection.Class.inc
  4. ' A singleton VBScript "factory" for ADODB.Connection to multiple databases
  5. ' Enhanced with separate error handling for creation and open operations
  6. '------------------------------------------------------------------------------
  7. ' Singleton holder
  8. Dim DatabaseConnection__Singleton
  9. Set DatabaseConnection__Singleton = Nothing
  10. ' Factory function
  11. Function DatabaseConnection()
  12. If DatabaseConnection__Singleton Is Nothing Then
  13. Set DatabaseConnection__Singleton = New DatabaseConnection_Class
  14. End If
  15. Set DatabaseConnection = DatabaseConnection__Singleton
  16. End Function
  17. '------------------------------------------------------------------------------
  18. ' Class definition
  19. '------------------------------------------------------------------------------
  20. Class DatabaseConnection_Class
  21. Private conn ' holds the ADODB.Connection instance
  22. '----------------------------------------
  23. ' Connect to an Access (.mdb/.accdb) file
  24. '----------------------------------------
  25. Public Function ConnectToAccessDatabase(dataSource, provider)
  26. If IsEmpty(provider) Or provider = "" Then
  27. provider = "Microsoft.Jet.OLEDB.4.0"
  28. End If
  29. Dim connStr
  30. connStr = "Provider=" & provider & ";" & _
  31. "Data Source=" & dataSource & ";" & _
  32. "Persist Security Info=False;"
  33. Set ConnectToAccessDatabase = Me.Connect(connStr)
  34. End Function
  35. '----------------------------------------
  36. ' Connect to SQL Server
  37. '----------------------------------------
  38. Public Function ConnectToSQLServer(server, database, uid, pwd, useTrusted)
  39. Dim connStr
  40. If useTrusted = True Then
  41. connStr = "Provider=SQLOLEDB;" & _
  42. "Server=" & server & ";" & _
  43. "Database=" & database & ";" & _
  44. "Trusted_Connection=Yes;"
  45. Else
  46. connStr = "Provider=SQLOLEDB;" & _
  47. "Server=" & server & ";" & _
  48. "Database=" & database & ";" & _
  49. "User ID=" & uid & ";" & _
  50. "Password=" & pwd & ";"
  51. End If
  52. Set ConnectToSQLServer = Me.Connect(connStr)
  53. End Function
  54. '----------------------------------------
  55. ' Connect via ODBC DSN
  56. '----------------------------------------
  57. Public Function ConnectToODBC(dsnName, uid, pwd)
  58. Dim connStr
  59. connStr = "DSN=" & dsnName & ";"
  60. If Not IsEmpty(uid) Then connStr = connStr & "UID=" & uid & ";"
  61. If Not IsEmpty(pwd) Then connStr = connStr & "PWD=" & pwd & ";"
  62. Set ConnectToODBC = Me.Connect(connStr)
  63. End Function
  64. '----------------------------------------
  65. ' Generic Connect: opens and returns an ADODB.Connection
  66. ' Includes separate handling for creation and open errors
  67. '----------------------------------------
  68. Public Function Connect(connectionString)
  69. On Error Resume Next
  70. ' Dispose previous connection if any
  71. If Not conn Is Nothing Then
  72. conn.Close
  73. Set conn = Nothing
  74. End If
  75. ' Create ADO Connection object
  76. Set conn = Server.CreateObject("ADODB.Connection")
  77. If conn Is Nothing Then
  78. Err.Clear
  79. On Error GoTo 0
  80. Err.Raise 50000, _
  81. "DatabaseConnection_Class.Connect", _
  82. "Could not create ADODB.Connection. Ensure ADO is installed and registered."
  83. End If
  84. ' Clear any prior errors before opening
  85. Err.Clear
  86. ' Open database connection
  87. conn.Open connectionString
  88. If Err.Number <> 0 Then
  89. Dim lastErrNum, lastErrDesc
  90. lastErrNum = Err.Number
  91. lastErrDesc = Err.Description
  92. Err.Clear
  93. On Error GoTo 0
  94. Err.Raise lastErrNum, _
  95. "DatabaseConnection_Class.Connect", _
  96. "Failed to open connection (" & connectionString & _
  97. ") - Error " & lastErrNum & ": " & lastErrDesc
  98. End If
  99. On Error GoTo 0
  100. Set Connect = conn
  101. End Function
  102. '----------------------------------------
  103. ' Close & clean up
  104. '----------------------------------------
  105. Public Sub Close()
  106. On Error Resume Next
  107. If Not conn Is Nothing Then
  108. conn.Close
  109. Set conn = Nothing
  110. End If
  111. On Error GoTo 0
  112. End Sub
  113. End Class
  114. %>

Powered by TurnKey Linux.