sql server语言 - SQL Azure and Powershell for DACPAC deploy



我正在使用powershell将基于Visual Studio 2012构建的dacpac部署到SQL Azure,并遇到了我认为可能与某些版本不兼容有关的问题。当我从视觉工作室执行此操作时,发布工作正常,但是当我使用Powershell执行此操作时会引发异常。

这是我在Powershell中所做的

[System.Reflection.Assembly]::Load("Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91") | out-null
[System.Reflection.Assembly]::Load("Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91") | out-null
[System.Reflection.Assembly]::Load("Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91") | out-null
[System.Reflection.Assembly]::Load("Microsoft.SqlServer.Management.Dac, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91") | out-null
Trap 
{  
  PrintException($_.Exception);
  $fileStream.Close()  
  return;  
}
$sqlServerFullName = $sqlServerName + ".database.windows.net"
$serverConnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($sqlServerFullName, $adminLogin, $admingPwd)
$serverconnection.Connect()
$dacstore = New-Object Microsoft.SqlServer.Management.Dac.DacStore($serverConnection)
$fileStream = [System.IO.File]::Open($dacpacPath,[System.IO.FileMode]::OpenOrCreate)
Write-Host "Reading contents from $dacpacPath..."
$dacType = [Microsoft.SqlServer.Management.Dac.DacType]::Load($fileStream)

上面代码的最后一行是崩溃并出现以下错误(内部异常值)并且没有继续

The stream cannot be read to construct the DacType.
There is an error in XML document (2, 2).
<DacType xmlns='http://schemas.microsoft.com/sqlserver/dac/Serialization/2012/0
2'> was not expected.

这是我正在使用的Powershell ISE的$PSVersionTable

Name                           Value                                           
----                           -----                                           
PSVersion                      2.0                                             
PSCompatibleVersions           {1.0, 2.0}                                      
BuildVersion                   6.1.7601.17514                                  
CLRVersion                     4.0.30319.17929                                 
WSManStackVersion              2.0                                             
PSRemotingProtocolVersion      2.1                                             
SerializationVersion           1.1.0.1     
想知道当我使用 Powershell 部署时,当我

使用 Visual Studio 2012 部署它时它工作正常时,什么会导致此问题

附言 - Powershell 部署使用我在互联网上找到的带有 dacpac 文件的相同脚本工作正常,该文件显然是针对 SQL 2005 版本的

如果您安装了最新的 SSDT 2015 (https://msdn.microsoft.com/en-us/mt186501)。我强烈推荐它,因为它有很多有用的发布配置文件选项,可以使用Visual Studio生成。

可以使用以下内容通过 Powershell 将文件发布到 Azure

$sqlpackage = "C:Program Files (x86)Microsoft Visual Studio 12.0Common7IDEExtensionsMicrosoftSQLDBDAC120sqlpackage.exe"
$dbserver = "<Azure DB Location>"  
$database = "<name of DB on Server>"
# UNC Paths
$dbProfile = "<Path to your Publish Profile>"
$mydacpac = "<location of the dacpac>" 
# Publish Command
& $sqlpackage /Action:Publish /tsn:$dbServer /tdn:$database /sf:$mydacpac/pr:$dbProfile /variables:myVariable=1

PS : 和工作方式类似于 Pradebban 提到的 Invoke-Expression

尝试使用 sqlpackage 发布.exe 和 发布.xml

Invoke-Expression = "C:Program Files (x86)Microsoft SQL Server110DACbinsqlpackage.exe /Action:Publish /Sourcefile:<.dacpac file_path> /pr:'<Publish.xml>'"

最新更新