簡體   English   中英

導入模塊 SQLServer 無法加載文件或程序集 Microsoft.SqlServer.ConnectionInfo 或 Microsoft.SqlServer.Smo

[英]Import-Module SQLServer cannot load file or assembly Microsoft.SqlServer.ConnectionInfo or Microsoft.SqlServer.Smo

我有一個 Powershell 腳本,它接收 CSV 數據並將其加載到 SQL Server 中,但看起來我在連接到服務器時遇到了一些問題,所以我退后一步並嘗試在沒有腳本其余部分的情況下建立連接。 我的腳本使用 Windows 身份驗證,當我調用“Import-Module SQLServer”時,出現以下錯誤:

PS C:\Users\Student> import-module sqlserver
add-type : Could not load file or assembly 'Microsoft.SqlServer.ConnectionInfo, Version=9.0.242.0, Culture=neutral,
PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
At C:\Users\Student\Documents\WindowsPowerShell\Modules\sqlserver\SQLServer.psm1:17 char:8
+ catch {add-type -AssemblyName "Microsoft.SqlServer.ConnectionInfo"}
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.AddTypeCommand

add-type : Could not load file or assembly 'Microsoft.SqlServer.Smo, Version=9.0.242.0, Culture=neutral,
PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
At C:\Users\Student\Documents\WindowsPowerShell\Modules\sqlserver\SQLServer.psm1:20 char:8
+ catch {add-type -AssemblyName "Microsoft.SqlServer.Smo"; $smoVersion  ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.AddTypeCommand

WARNING: SMOExtended not available

PS C:\Users\Student> cd SQLSERVER:\SQL
cd : Cannot find drive. A drive with the name 'SQLSERVER' does not exist.
At line:1 char:1
+ cd SQLSERVER:\SQL
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SQLSERVER:String) [Set-Location], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

前兩個 FileNotFoundExceptions 僅在我第一次運行 Import-Module 命令時出現,但我仍然在調用 Set-Location 時收到 ObjectNotFound 錯誤。 更有趣的是,我有另一個功能版本的腳本,它使用 SQL Server 身份驗證通過 SqlConnection 對象連接到數據庫。 當我運行此腳本然后返回並運行 Windows 身份驗證腳本時,我沒有收到以前的錯誤,並且能夠毫無問題地連接到 SQL Server。

我已經嘗試過重新安裝 SQLServer 模塊並通過 .NET CLI 安裝 Microsoft.SqlServer.SqlManagementObjects,如https://www.nuget.org/packages/Microsoft.SqlServer.SqlManagementObjects

此外,知道 Microsoft.SqlServer.ConnectionInfo.dll 和 Microsoft.SqlServer.Smo.dll 都位於 C:\\Program Files (x86)\\Microsoft SQL Server\\120\\SDK\\Assemblies 可能也是很好的信息。 是否有可能因為它們不在正確的位置而無法加載它們?

謝謝你的幫助。

編輯:

我發現 Microsoft.SqlServer.Smo 位於 C:\\Program Files\\Microsoft SQL Server\\140\\SDK\\Assemblies(注意 Program Files 而不是 Program Files (x86)),但 ConnectionInfo 程序集不是。 我正在使用 SQL Server 2017,並驗證我正在運行 x64 版本,所以我認為這可能是問題所在。 沒有 ConnectionInfo 程序集是否也會導致我遇到的 Smo 加載失敗?

嘗試使用

Set-Location -Path $PSHOME
Add-Type -AssemblyName *sqlserver* -PassThru

參考: https : //docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-7.1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM