連接字符串是應用與數據庫握手的鑰匙。掌握四大核心屬性族與兩大認證模式,消除連接失敗猜謎遊戲。
| 標準名 | 常用別名 | 含義 |
|---|---|---|
Data Source | Server, Addr, Address, Network Address | 服務器實例(如 .\SQLEXPRESS, 192.168.1.100,1433) |
Initial Catalog | Database, DB | 初始數據庫 |
Integrated Security | Trusted_Connection | Windows 集成認證 (SSPI/True/Yes) |
User ID | UID, Username | SQL 登錄用戶名 |
Password | PWD | SQL 登錄密碼 |
-- SQL 認證
"Server=localhost;User ID=sa;Password=xxx;Database=Northwind"
-- Windows 集成認證(推薦)
"Server=localhost;Integrated Security=SSPI;Database=Northwind"
"Server=localhost;Trusted_Connection=Yes;Database=Northwind"
Connection Timeout=30:連接超時秒數Max Pool Size=100:連接池上限Application Name=MyApp:識別來源,便利 Profiler/Extended Events 篩選Encrypt=True;TrustServerCertificate=False:強制 TLS 加密MultipleActiveResultSets=True (MARS):單連接多結果集「連接字符串不再是黑盒」—— 理解每個屬性含義,排查連接問題從「試錯」變為「診斷」。