在除錯資料庫連線、檢視驅動程式支援屬性、確認連線字串生效狀態時,列舉 ADODB.Connection 物件的 Properties 集合極為實用。
<%@ Language=VBScript %>
<%
Option Explicit
Dim objConn, intCount, objProp, vbCRLF
vbCRLF = Chr(10)
Response.Write "<pre>" & vbCRLF
Response.Write "資料庫連線屬性清單" & vbCRLF & vbCRLF
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "DSN=VicTest" ' 請替換為實際 DSN 或連線字串
Response.Write "編號 | 名稱 | 值" & vbCRLF
Response.Write String(60, "-") & vbCRLF
intCount = 0
For Each objProp In objConn.Properties
intCount = intCount + 1
Response.Write intCount & " | " & objProp.Name & " | " & objProp.Value & vbCRLF
Next
Response.Write String(60, "-") & vbCRLF
Response.Write "共 " & intCount & " 個屬性" & vbCRLF
Response.Write "</pre>"
' 清理資源
objConn.Close
Set objConn = Nothing
%>
編號 | 名稱 | 值 ------------------------------------------------------------ 1 | Access Mode | 0 2 | Auto Translate | True 3 | Connect Timeout | 15 4 | Current Catalog | 5 | Data Source | VicTest 6 | Extended Properties | 7 | Initial Catalog | pubs 8 | Integrated Security | 9 | Locale Identifier | 1028 10 | Mode | 0 11 | Password | 12 | Persist Security Info | False 13 | Prompt | 4 14 | User ID | sa ... (更多驅動特定屬性) 共 30 個屬性
| 屬性名稱 | 用途 |
|---|---|
| Data Source | 資料來源名稱(DSN 或伺服器位址) |
| Initial Catalog | 預設資料庫名稱 |
| User ID / Password | 認證帳密(若連線字串含密碼,此處可能顯示遮罩) |
| Connect Timeout | 連線逾時秒數 |
| Mode | 開啟模式(讀/寫/讀寫/共享) |
| Isolation Level | 交易隔離等級 |
If objConn.Properties("Mode").Value <> 3 Then ' 3 = adModeReadWrite
Response.Write "警告:連線非讀寫模式,可能無法更新資料!"
End If
透過 Connection.Properties 集合,開發者可完整掌握 ADO 連線的實際運行參數,對排查「為何唯讀」、「連線逾時設定是否生效」等問題極具參考價值。