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

SQL Server 客戶端連接組件精簡部署:核心 DLL、注冊表、連接字串最小化棲指南

     編輯:關於SqlServer

核心問題:為什麼「只複製 DLL」在新機器失敗?

SQL Server 客戶端連接不僅需 DLL,還依賴:

  1. 網絡庫 (Net-Libraries):`dbnetlib.dll` (TCP/IP)、`dbnmpntw.dll` (Named Pipes)、`dbmsrpcn.dll` (Multiprotocol/RPC)、`dbmsadsn.dll` (AppleTalk)、`dbmsspxn.dll` (IPX/SPX)。
  2. 核心通信層:`ntwdblib.dll` (DB-Library 兼容)、`msvcr71/80/90/100/110/120/140.dll` (CRT 運行庫)。
  3. 注冊表配置:`HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo` 別名映射、`HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SNI` 協議順序。
  4. 系統組件:MDAC 2.8+ / Windows Data Access Components (WDAC) / SQL Native Client (SNAC) / ODBC Driver / OLE DB Driver。

最小化部署清單 (x86/x64 必須匹配目標進程位數)

文件來源用途必選
`sqlncli10.dll` / `sqlncli11.dll` / `msodbcsql17.dll` / `msoledbsql.dll`SQL Native Client / ODBC Driver / OLE DB Driver 安裝包現代首選數據訪問提供程序 (TDS 8.0+、加密、MARS、TVP)✅
`dbnetlib.dll`SQL Server 安裝介質 / 系統目錄TCP/IP Named Pipes 兼容層 (舊應用)⚠️ 僅舊應用
`ntwdblib.dll` (2000.80.x)SQL Server 2000 介質DB-Library 極舊應用 (PB/Delphi/VB6)⚠️ 極舊應用
CRT 運行庫 (vc_redist.x86/x64.exe)Microsoft 官網所有 DLL 依賴基礎✅

零注冊表部署:連接字串內嵌別名

無需寫入 `HKLM\...\ConnectTo`,直接在連接字串指定協議與端口:

-- TCP/IP 直連 (推薦)
Server=tcp:192.168.1.100,1433;Database=MyDB;Uid=User;Pwd=Secret;Encrypt=TrustServerCertificate;

-- Named Pipes (內網低延遲)
Server=np:\\DBServer\pipe\sql\query;Database=MyDB;Trusted_Connection=yes;

-- 別名 (需客戶端配置別名或 SQL Native Client 支持)
Server=MyAlias;Database=MyDB;...

PowerBoot 腳本:一鍵檢測並修復客戶端環境

#Requires -RunAsAdministrator
param($TargetDir = "C:\App\Bin")

# 1. 確保 VC++ Redist
if (-not (Test-Path "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64")) {
    Write-Host "Installing VC++ 2015-2022 Redist..." -Fore Yellow
    Start-Process -Wait -FilePath "vc_redist.x64.exe" -ArgumentList "/install", "/quiet", "/norestart"
}

# 2. 複製核心 DLL (假設已放在腳本目錄下 \Dlls)
$Dlls = @("sqlncli11.dll","msodbcsql17.dll","msoledbsql.dll","dbnetlib.dll")
foreach ($d in $Dlls) {
    if (Test-Path ".\Dlls\$d") {
        Copy-Item ".\Dlls\$d" -Destination $TargetDir -Force
        Write-Host "Copied $d"
    }
}

# 3. 驗證連接 (需引用 System.Data.dll)
Add-Type -AssemblyName System.Data
$cs = "Server=tcp:192.168.1.100,1433;Database=master;Uid=sa;Pwd=Test123;Connect Timeout=5;Encrypt=TrustServerCertificate;"
try {
    $conn = New-Object System.Data.SqlClient.SqlConnection $cs
    $conn.Open()
    Write-Host "Connection OK, ServerVersion: $($conn.ServerVersion)" -Fore Green
    $conn.Close()
} catch {
    Write-Host "Connection FAILED: $($_.Exception.Message)" -Fore Red
}

常見報錯與對應修復

錯誤信息根因修復
`Provider not found. It may not be properly installed.`OLE DB/ODBC 驅動未安裝/註冊安裝 `MSOLEDBSQL.msi` / `msodbcsql.msi` 或複製 DLL + `regsvr32` (OLE DB)
`SQL Network Interfaces: Error 26 - Error Locating Server/Instance Specified`實例名錯誤/瀏覽器服務未啟動/防火牆用 `tcp:IP,Port` 直連、啟動 SQL Browser、開放 1434 UDP
`The client and server cannot communicate, because they do not possess a common algorithm`TLS 版本不匹配 (客戶端舊、服務器強制 TLS 1.2+)更新驅動至最新、啟用 `Encrypt=TrustServerCertificate`、或服務器降級 TLS
`LoadLibrary failed for sqlncli11.dll`位數不匹配 (x86 進程載入 x64 DLL 或反向)編譯目標平台與 DLL 位數一致、或安裝雙版本驅動

現代化建議:棄用舊組件,擁抱 Microsoft Data Access Components (MDAC) 後繼者

  1. 新開發:強制使用 **Microsoft ODBC Driver 17/18 for SQL Server** / **Microsoft OLE DB Driver for SQL Server (MSOLEDBSQL)** / **.NET SqlClient (System.Data.SqlClient / Microsoft.Data.SqlClient)**。
  2. 遷移存量:逐步替換連接字串 `Provider=SQLOLEDB` → `MSOLEDBSQL`,`Driver={SQL Server}` → `ODBC Driver 17 for SQL Server`。
  3. 容器化:基礎鏡像預裝 `msodbcsql17` + `msoledbsql`,應用鏡像無需額外 DLL。
  4. 雲原生:Azure SQL / AWS RDS 連接強制加密,驅動需支持 `Encrypt=Strict`、`TrustServerCertificate=False`、托管身份認證。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved