Search code examples
powershellmsbuildsql-server-data-tools

Is there a way to call SSDT DacServices without a connectionstring


I have a Source DacPac and a Target DacPac. With powershell I would like to compare both and generate a upgrade script. This per se is simple for all cases with DacServices as long as there is a valid Connection String. However, for my CD pipeline, I do not know the eventual connection string nor do I wish to know. And all I want is for the script to generate the SQL.

I tried this out with sqlpackage.exe by sending args as follows

$args = @( "/a:Script", "/sf:$PathToDacpacSource", "/tf:$PathToDacpacTarget", "/op:$outputFile", "/tdn:$TargetDatabaseName" )

and this works fine. so far I had managed by adding just a dependency to DacServices via [Microsoft.SqlServer.DacFx.x64], but this inability to generate scripts without a connection string has forced me to add the dependency on SQLPackage.exe via [Microsoft.Data.Tools.Msbuild] too

The constructors for DacServices all require a ConnectionString

Has anyone else encountered a similar situation and any solutions?

or Do you exclusively use SQLPackage at the deployment side to overcome this issue?


Solution

  • There is a static version of GenerateDeployScript that does not need a connection string as you don't call the constructor for a static method, it isn't very clear in the msdn documentation but the "S" which looks like it is floating means it is static :)

    https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dac.dacservices.generatedeployscript(v=sql.120).aspx#M:Microsoft.SqlServer.Dac.DacServices.GenerateDeployScript%28Microsoft.SqlServer.Dac.DacPackage,Microsoft.SqlServer.Dac.DacPackage,System.String,Microsoft.SqlServer.Dac.DacDeployOptions%29

    ` public static string GenerateDeployScript( DacPackage sourcePackage, DacPackage targetPackage, string targetDatabaseName, DacDeployOptions options = null )