vsdbcmdexe overriding variable value

When you deploy a database project with the command line utility vsdbcmd.exe you may want to change the value of some variables of the project. Suppose you’ve created a variable called Path1

image

And you have used this variable to specify the location of the files

1
2
ALTER DATABASE [$(DatabaseName)]
    ADD FILE (NAME = [Northwind], FILENAME = '$(Path1)$(DatabaseName).mdf',

Now you want to decide at deploy time the physical location of your database. The main problem is that vsdbcmd.exe takes deploy variable from the Database.sqlcmdvars file, so if you want to change a value of a variable you have two options. The first is using some xpath knowledge to change the value in the Database.sqlcmdvars, since it is a simple xml file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?xml version="1.0" encoding="utf-8"?>
<SqlCommandVariables xmlns="urn:Microsoft.VisualStudio.Data.Schema.Project.SqlCmdVars">
  <Version>1</Version>
  <Properties>
    <Property>
      <PropertyName>Path1</PropertyName>
      <PropertyValue>C:\Temp\</PropertyValue>
    </Property>
   ...
</SqlCommandVariables>

With some Xpath knowledge you can change the value of the property Path1, suppose you change to v:\ (v is my ramdisk), then you run the vsdbcmd.exe and you will find that files are created indeed in the correct location

image

If you do not like using XPath to change the configuration file, you can use a series of preconfigured files and swap before calling vsdbcmd.exe. Sadly the documentation states that you have an option to choose variable file

image

But actually you get an error when you use it.

Alk.

Tags: Visual Studio .Net