Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a connection string as a parameter to MsBuild to perform SQL Schema Compare?

The SQL Server Data Tools team blog mentions that it is now possible to use MsBuild to perform a schema comparison of two DacPacs or databases. However, they do not mention exactly how to pass in the connection string to the source and target database. If I set the parameter /p:source="my connection string" I get the error:

MSBUILD : error MSB4177: Invalid property. The name "Initial Catalog" contains an invalid character " ".

The command-line my PowerShell script sends to msbuild is:

msbuild ".\SchemaCompare.proj" /t:SqlSchemaCompare 
/p:source="$sourceConnString" /p:target="$targetConnString" 
/p:XmlOutput="$schemaCompareReportPath" 

where the SchemaCompare.proj contains the content suggested on the SQL Server Data Tools team blog

like image 468
John Tunnicliffe Avatar asked Sep 21 '15 12:09

John Tunnicliffe


People also ask

How do I compare two db schemas?

To compare database definitions. On the Tools menu, select SQL Server, and then click New Schema Comparison. Alternatively, right-click the TradeDev project in Solution Explorer, and select Schema Compare. The Schema Compare window opens, and Visual Studio automatically assigns it a name such as SqlSchemaCompare1 .

What is connection string parameter?

To connect to a database, the application provides a connection string which specifies parameters such as the host, the username, the password, etc. Connection strings have the form keyword1=value; keyword2=value; and are case-insensitive.

How do I find the connection string in SQL Server?

Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.

Where do I put connection string in web config?

<connectionStrings> <add name="dbconnection" connectionString="Data Source=Soumalya;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System. Data. SqlClient" />


1 Answers

It turns out that you have to replace the semicolons in the connection string with %3B, like so:

$sourceConnString = $sourceConnString.Replace(";", "%3B")

Explanation: This prevents delimiter collisions between the SQL Server ConnectionString value and the MSBuild /p (a.k.a. /properties) value that contains it. Absent this URL-encoding, MSBuild will tokenize the ConnectionString on semicolons rather than passing on the whole value intact (for the SQL Server client libraries to tokenize). This also applies to the Properties attribute of the MSBuild Task.

References:

  • MSBuild Special Characters
  • How to: Escape Special Characters in MSBuild
like image 192
Steven Green Avatar answered Sep 29 '22 03:09

Steven Green