Search code examples
.netsql-serversmodacpac

Is it possible to publish a dacpac file using Microsoft.Sqlserver.Management.Smo?


I've looked around but am unable to find anything. I do not have access to

Microsoft.SqlServer.Dac

, unfortunately, and am unable to download it onto the machine I'm working on due to permissions.

EDIT Should also mention I'm running VS 2012 with SP4 and SQL Server 2008. My installation of VS has SSDT installed, but for some reason the Microsoft.SqlServer.Dac DLL was not installed.


Solution

  • You can publish a DacPac using SMO with powershell, below is a copy of the example from the documentation.

    ## Set a SMO Server object to the default instance on the local computer.  
    CD SQLSERVER:\SQL\localhost\DEFAULT  
    $srv = get-item .  
    
    ## Open a Common.ServerConnection to the same instance.  
    $serverconnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($srv.ConnectionContext.SqlConnectionObject)  
    $serverconnection.Connect()  
    $dacstore = New-Object Microsoft.SqlServer.Management.Dac.DacStore($serverconnection)  
    
    ## Load the DAC package file.  
    $dacpacPath = "C:\MyDACs\MyApplication.dacpac"  
    $fileStream = [System.IO.File]::Open($dacpacPath,[System.IO.FileMode]::OpenOrCreate)  
    $dacType = [Microsoft.SqlServer.Management.Dac.DacType]::Load($fileStream)  
    
    ## Subscribe to the DAC deployment events.  
    $dacstore.add_DacActionStarted({Write-Host `n`nStarting at $(get-date) :: $_.Description})  
    $dacstore.add_DacActionFinished({Write-Host Completed at $(get-date) :: $_.Description})  
    
    ## Deploy the DAC and create the database.  
    $dacName  = "MyApplication"  
    $evaluateTSPolicy = $true  
    $deployProperties = New-Object Microsoft.SqlServer.Management.Dac.DatabaseDeploymentProperties($serverconnection,$dacName)  
    $dacstore.Install($dacType, $deployProperties, $evaluateTSPolicy)  
    $fileStream.Close()