首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> SQL Server 2005

SQL Server 2005 連接字串完整參考指南

     編輯:SQL Server 2005

連接字串關鍵字速查

類別關鍵字別名說明
伺服器Data Source, Server, Addr, Address, Network Address伺服器名稱或 IP,支援 主機\實例名 格式
資料庫Initial Catalog, Database目標資料庫名稱
Windows 驗證Integrated Security, Trusted_ConnectionSSPI / true / yes 啟用
SQL 驗證User ID, Password, Uid, PwdSQL Server 登入帳密

.NET (SqlConnection) 常用範本

標準 SQL 驗證

Data Source=myServerAddress;Initial Catalog=myDataBase;
User Id=myUsername;Password=myPassword;

Windows 整合驗證(推薦)

Data Source=myServerAddress;Initial Catalog=myDataBase;
Integrated Security=SSPI;
-- 或
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

優勢:無密碼明文、Kerberos/NTLM 雙重跳躍支援、連線池效能較佳。

指定執行個體

Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=True;

MARS (Multiple Active Result Sets)

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
MultipleActiveResultSets=True;

需 ADO.NET 2.0+,允許單一連線同時開啟多個 DataReader。

非同步處理

Server=myServerAddress;Database=myDataBase;Integrated Security=True;
Asynchronous Processing=True;

本地資料庫檔案附加

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\mydb.mdf;
Database=dbname;Trusted_Connection=Yes;User Instance=True;

資料庫鏡像容錯移轉

Data Source=myServerAddress;Failover Partner=myMirrorServer;
Initial Catalog=myDataBase;Integrated Security=True;

ODBC (SQL Native Client) 範本

標準連線

Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;

受信連線

Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;
Trusted_Connection=yes;

OLE DB (SQLNCLI) 範本

Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;
-- 受信
Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;

常見問題與提示

  • SQL Express 實例名:Server=主機名\SQLEXPRESS
  • 連接埠指定:Server=192.168.1.100,1433(逗號分隔)
  • 網路庫指定:Network Library=DBMSSOCN 強制 TCP/IP
  • 加密連線:Encrypt=yes(需伺服器端憑證)
  • 連線逾時:Connection Timeout=30(秒)
  • 應用程式名稱:Application Name=MyApp 便於 Profiler 追蹤

安全性最佳實務

  1. 優先使用 Windows 整合驗證,避免程式碼/設定檔含密碼。
  2. 應用程式使用最低權限的 SQL 登入(僅授予必要資料庫角色)。
  3. 啟用強制加密(伺服器端設定 Force Encryption = Yes)。
  4. 連線字串勿硬編碼,放入 web.config connectionStrings 區段並加密(aspnet_regiis -pef)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved