In addition to performing the build, deploy, or clean actions from within the Visual Studio user interface, you can also perform these actions at a command prompt by using MSBuild.exe. You can specify Build, Deploy, Rebuild, Clean, SQLBuild, SQLDeploy, and CleanProject targets. By default, the build and deploy processes use the project properties that are defined within the database project (in the .dbproj file or the .dbproj.user file). However, you can override these properties at a command prompt or from within a response file.
Important Note: |
|---|
You should close Visual Studio before you perform a command-line build. If you perform a command-line build when Visual Studio is running, some errors might not be caught. |
Command-Line Syntax
You can build the database project at a command prompt by using the following examples of syntax:
- MSBuild /target:Build MySolutionName.sln
This example performs the build action on the solution that is named MySolutionName.sln by using the project properties that are specified in the project files that the solution contains. If the solution contains multiple database projects, they are built together with everything else in the solution. The build action will generate the script that is required to create or update the target database, but the script will not be deployed. You can also build a specific database project. The Build target includes pre-deployment and post-deployment scripts in the generated build script.
- MSBuild /target:SQLBuild MyProjectName.dbproj
This example also performs a build-only action. You can use this syntax to build only a single database project that is named MyProjectName.dbproj. The project properties within the .dbproj file are used to assemble the build script. The script is not deployed. The SQLBuild target does not include the pre-deployment or post-deployment scripts in the generated build script.
- MSBuild /target:Build /p:BuildScriptName=MyScriptName.sql MyProjectName.dbproj
This example also performs a build-only action. You can use this syntax to build only a single database project that is named MyProjectName.dbproj. The project properties within the .dbproj file are used to assemble the build script. The command line overrides the BuildScriptName property and specifies a different name for the output script. You might use this approach if you want to deploy the build script to multiple servers. The script is not deployed.
- MSBuild /target:Deploy /property:TargetDatabase=UpdatedTargetDatabase;TargetConnectionString="Data Source=(local)\SQLEXPRESS;Integrated Security=True;Pooling=False" MyProjectName.dbproj
This example demonstrates how to deploy the database project while overriding the target database name and connection string.
- MSBuild /target:Deploy /p:DeploymentConfiguration=DeployPath\AlternateDeploymentConfiguration.deploymentconfig /p:SqlCommandVars=VarPath\AlternateVariables.sqlcmdvars /property:TargetDatabase=UpdatedTargetDatabase;TargetConnectionString="Data Source=(local)\SQLEXPRESS;Integrated Security=True;Pooling=False" MyProjectName.dbproj
This example demonstrates how to deploy the database project while specifying a different target database and connection string and overriding the deployment configuration and deployment variables.
- MSBuild /target:Deploy /property:BuildScriptName=MyScriptName.sql /property:outdir=BuildScriptPath /property:TargetDatabase=UpdatedTargetDatabase;TargetConnectionString="Data Source=InstanceName\DatabaseName;Integrated Security=True;Pooling=False" ProjectPath\MyProjectName.dbproj
This example demonstrates how to deploy a database from a computer other than the one on which the build occurred. For example, you might use this syntax if you have a central build computer that creates a build script every night. You must specify the name of the build script, the path where the build script can be found (outdir), the target database, and the path and file name of the database project.
- MSBuild @dbbuild.arf MyProjectName.dbproj
This example demonstrates how to use a response file to provide command-line arguments. The file, dbbuild.arf, can contain any valid arguments for MSBuild, including those that override project properties.
- MSBuild /target:Rebuild MyProjectName.dbproj
This example rebuilds the specified project or solution, even if it has not changed since the last time it was built.
- MSBuild /target:Clean MyProjectName.dbproj
This example demonstrates how to delete any existing build scripts. In most cases, you would follow this action with another build or deploy action.
Note: |
|---|
You can abbreviate /target: as /t: and /property: as /p:. |
For more information, see MSBuild Command Line Reference.
For more information about response files, see this topic on the Microsoft Web site: MSBuild Response Files.
Note: |
|---|
To run MSBuild.exe, you must either use the Visual Studio Command Prompt, or you must run the vsvars32.bat batch file. You can find this batch file in the folder that the %VS80COMNTOOLS% environment variable specifies. |
Project Properties
Overriding Deployment Manifest Properties
You can override the default deployment properties (such as deployment configuration, connection string, or SQLCMD variables) when you deploy from the command line. You might choose to do this if you want to deploy a .dbschema file into multiple environments.
For example, if you want to deploy a schema defined in EnterpriseDB.dbproj into development, test, and production environments, you could use the following command lines:
Deployment Environment
MSBuild EnterpriseDB.dbproj /t:Deploy /p:DeploymentConfigurationFile=sql\debug\Development.sqldeployment /p:SqlCommandVariablesFile=sql\debug\Development.sqlcmdvars /p:TargetConnectionString="Data Source=DEV\sql2008;Integrated Security=true;Pooling=false"
Test Environment
MSBuild EnterpriseDB.dbproj /t:Deploy /p:DeploymentConfigurationFile=sql\debug\UserTest.sqldeployment /p:SqlCommandVariablesFile=sql\debug\UserTest.sqlcmdvars /p:TargetConnectionString="Data Source=USERTEST\sql2008;Integrated Security=true;Pooling=false"
Production Environment
MSBuild EnterpriseDB.dbproj /t:Deploy /p:DeploymentConfigurationFile=sql\debug\Production.sqldeployment /p:SqlCommandVariablesFile=sql\debug\Production.sqlcmdvars /p:TargetConnectionString="Data Source=PRODUCTION\sql2008;Integrated Security=true;Pooling=false"
In each environment, you provide a different deployment configuration file, a different SQLCMD variables file, and a different connection string.