萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MS SQL 資料庫

SQL Server 連接字串完整參考:Data Source、Integrated Security、Trusted_Connection 等關鍵字詳解

四大核心關鍵字類別

類別常用關鍵字(同義詞)說明
伺服器Data Source, Server, Addr, Address, Network Address實例名稱:Server=MyServer\Instance;預設實例可省略實例名
資料庫Initial Catalog, Database初始資料庫上下文
Windows 整合驗證Integrated Security, Trusted_ConnectionIntegrated 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,單連線多結果集)
copyright © 萬盛學電腦網 all rights reserved