无法识别术语“ Microsoft.SqlServer.Dac.BacPackage :: Load”

时间:2018-08-07 15:21:44

标签: powershell azure-sql-database sql-server-2016

我已经从azure sql数据库创建了一个bacpac,并将其从blob本地复制到blob。

我在powershell中运行此代码

Add-Type -path "C:\Program Files (x86)\Microsoft SQL Server\140\DAC\bin\Microsoft.SqlServer.Dac.dll"
$restoredDatabaseName = 'AuditTest'
$bacpacFile = "H:\backup\audit\audit.bacpac"
$conn = "Data Source=.;Initial Catalog=master;Connection Timeout=0;Integrated Security=True;"

$importBac = New-Object Microsoft.SqlServer.Dac.DacServices $conn
$loadBac = Microsoft.SqlServer.Dac.BacPackage::Load($bacpacFile)
$importBac.ImportBacpac($loadBac, $restoredDatabaseName)

#Clean up
$loadBac.Dispose()

找回此错误:

Microsoft.SqlServer.Dac.BacPackage::Load : The term 'Microsoft.SqlServer.Dac.BacPackage::Load' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again.

我还尝试使用导入数据层向导并出现以下错误。

Could not load file or assembly 'Microsoft.SqlServer.Dac, Version=13.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. (Microsoft.SqlServer.Management.Dac.DacWizard)

这让我发疯了,我发现了一些有关注册表黑客的事情,例如向导错误,对于Powershell根本没有帮助。

SQL Azure数据库PRS1 在Azure VM中还原到SQL 2016。

1 个答案:

答案 0 :(得分:1)

调用静态方法时,完整类型名称放在方括号中。

应该是:

Add-Type -path "C:\Program Files (x86)\Microsoft SQL Server\140\DAC\bin\Microsoft.SqlServer.Dac.dll"
$restoredDatabaseName = 'AuditTest'
$bacpacFile = "H:\backup\audit\audit.bacpac"
$conn = "Data Source=.;Initial Catalog=master;Connection Timeout=0;Integrated Security=True;"

$importBac = New-Object Microsoft.SqlServer.Dac.DacServices $conn
$loadBac = [Microsoft.SqlServer.Dac.BacPackage]::Load($bacpacFile)
$importBac.ImportBacpac($loadBac, $restoredDatabaseName)

#Clean up
$loadBac.Dispose()