| 技術 | 驅動/提供者 | 特點 | 推薦場景 |
|---|---|---|---|
| ODBC | SQL Server Native Client / ODBC Driver 17/18 | 跨平台標準、廣泛相容 | 舊系統維護、異構環境 |
| OLE DB (SQLNCLI) | SQLNCLI11 / SQLOLEDB / MSOLEDBSQL | 原生效能佳、支援 SQL Server 完整功能 | Windows 生產環境首選 |
<%
Dim conn, connStr
connStr = "Driver={SQL Server Native Client 11.0};" & _
"Server=localhost;" & _
"Database=MyDB;" & _
"Uid=sa;" & _
"Pwd=your_password;"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connStr
%>
<%
Dim conn, connStr
connStr = "Provider=SQLNCLI11;" & _
"Data Source=localhost;" & _
"Initial Catalog=MyDB;" & _
"User ID=sa;" & _
"Password=your_password;"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connStr
%>
connStr = "Provider=SQLOLEDB.1;" & _
"Data Source=localhost;" & _
"Initial Catalog=MyDB;" & _
"User ID=sa;" & _
"Password=your_password;"
<%
Dim conn, rs, sql
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connStr
sql = "SELECT TOP 10 UserID, UserName, Email FROM Users WHERE IsActive = 1 ORDER BY RegDate DESC"
Set rs = conn.Execute(sql)
If Not rs.EOF Then
Do While Not rs.EOF
Response.Write rs("UserName") & " <" & rs("Email") & "><br/>"
rs.MoveNext
Loop
End If
' 嚴格釋放順序
rs.Close : Set rs = Nothing
conn.Close : Set conn = Nothing
%>
Data Source / Server:伺服器位址 (localhost、IP、實例名 SERVER\INSTANCE)Initial Catalog / Database:資料庫名稱Integrated Security=SSPI:Windows 整合驗證 (免帳密,需應用程式池身分有權限)Trusted_Connection=Yes:同整合驗證Application Name=MyApp:識別連接來源,利於 SQL Profiler 追蹤新專案請優先使用 Microsoft OLE DB Driver for SQL Server (MSOLEDBSQL) 或 ODBC Driver 17/18 for SQL Server,支援 Always Encrypted、可用群組、UTF-8 等新特性,並持續獲得安全性更新。