四大核心關鍵字類別
| 類別 | 常用關鍵字(同義詞) | 說明 |
| 伺服器 | Data Source, Server, Addr, Address, Network Address | 實例名稱:Server=MyServer\Instance;預設實例可省略實例名 |
| 資料庫 | Initial Catalog, Database | 初始資料庫上下文 |
| Windows 整合驗證 | Integrated Security, Trusted_Connection | Integrated Security=SSPI 或 Trusted_Connection=Yes/True,使用當前 Windows 身分 |
| SQL Server 驗證 | User ID, UID, Password, PWD | 指定 SQL 登入帳密 |
兩種常見連接字串範本
SQL 驗證(含帳密)
Server=localhost;Database=Northwind;User ID=sa;Password=YourStrongPwd!;
Windows 整合驗證(推薦生產環境)
Server=localhost;Database=Northwind;Integrated Security=SSPI;
Server=localhost;Database=Northwind;Trusted_Connection=Yes;
為何推薦 Windows 整合驗證?
- 安全性高:無明文帳密在程式碼/設定檔,支援 Kerberos/NTLM 雙重保障;
- 效能佳:避免 SQL 驗證的額外驗證交握;
- 維運簡單:帳密變更無需改連接字串,群組原則統一管理;
- 連線池複用:同一進程內相同字串自動複用連線池。
進階常用參數
Connection Timeout=30(秒,預設 15)
Max Pool Size=100 / Min Pool Size=0
Application Name=MyApp(方便 Profiler/Extended Events 篩選)
Encrypt=True;TrustServerCertificate=False(強制 TLS 加密傳輸)
MultipleActiveResultSets=True(MARS,單連線多結果集)