Visual Studio 2010数据库项目部署到不同的环境

时间:2023-01-17 10:46:22

I've got a Database project which works fine for my local MSSQL 2008 database.

我有一个适合本地MSSQL 2008数据库的数据库项目。

It has a script under scripts/post-deployment that inserts standing data configuration into a settings table, and other tables. I've got 1 file for each table, e.g. a Setting.sql file to insert data into the Settings table.

它在脚本/后部署下有一个脚本,该脚本将持续的数据配置插入到设置表和其他表中。我为每个表准备了一个文件,例如一个设置。将数据插入设置表的sql文件。

The settings will be different depending on the database I deploy to.

根据部署到的数据库,设置将有所不同。

How can I script this? Basicaly I would like to be able to have say 2 files,

我怎么写这个呢?基本上,我想要有两个文件,

Prod.Setting.sql and Dev.Setting.sql and VS 2010 would use the appropriate script depending on what database (environment) I am deploying to.

Prod.Setting。sql和Dev.Setting。sql和VS 2010将根据部署到的数据库(环境)使用适当的脚本。

1 个解决方案

#1


3  

Completely doable but there two options and a few steps involved. You can have a complete duplicate set of scripts, one for each configuration. Or, you can have one set of scripts, the contents of which take into account the configuration you are using. I'll go with the first, and I'll keep it simple.

完全可行,但有两个选择和几个步骤。您可以有一套完整的脚本,一个用于每个配置。或者,您可以有一组脚本,其中的内容考虑到您正在使用的配置。我先讲第一个,尽量简单。

Create two solution configurations, or use Debug and Release if you like. I'll use these for the example.

创建两个解决方案配置,或者使用调试和发布。我将用这些作为例子。

For each configuration, create a new .sqlcmdvars file.

对于每个配置,创建一个新的.sqlcmdvars文件。

Database_Release.sqlcmdvars
Database_Debug.sqlcmdvars

Switch your solution configuration to each and in the database project properties change the variables file drop down to point at the corresponding file you created.

将您的解决方案配置切换到每个数据库项目属性中,将变量文件向下拉到您创建的相应文件上。

In each of those files you can define variables to be used during deployment. Create a new variable in each one

在每个文件中,您可以定义在部署期间使用的变量。在每个变量中创建一个新变量

$(DeploymentConfiguration)

And set its value in each one to either Debug or Release

并将其值分别设置为Debug或Release

Then in any of your Pre or Post deployment scripts you can do something like so:

然后,在任何部署前或部署后脚本中,您都可以这样做:

IF '$(DeploymentConfiguration)' = 'Debug'
BEGIN
PRINT 'Executing Debug deployment'
:r .\Debug\SomeNeededScript.sql
END

IF '$(DeploymentConfiguration)' = 'Release'
BEGIN
PRINT 'Executing Release deployment'
:r .\Release\Anotherscript.sql
END

#1


3  

Completely doable but there two options and a few steps involved. You can have a complete duplicate set of scripts, one for each configuration. Or, you can have one set of scripts, the contents of which take into account the configuration you are using. I'll go with the first, and I'll keep it simple.

完全可行,但有两个选择和几个步骤。您可以有一套完整的脚本,一个用于每个配置。或者,您可以有一组脚本,其中的内容考虑到您正在使用的配置。我先讲第一个,尽量简单。

Create two solution configurations, or use Debug and Release if you like. I'll use these for the example.

创建两个解决方案配置,或者使用调试和发布。我将用这些作为例子。

For each configuration, create a new .sqlcmdvars file.

对于每个配置,创建一个新的.sqlcmdvars文件。

Database_Release.sqlcmdvars
Database_Debug.sqlcmdvars

Switch your solution configuration to each and in the database project properties change the variables file drop down to point at the corresponding file you created.

将您的解决方案配置切换到每个数据库项目属性中,将变量文件向下拉到您创建的相应文件上。

In each of those files you can define variables to be used during deployment. Create a new variable in each one

在每个文件中,您可以定义在部署期间使用的变量。在每个变量中创建一个新变量

$(DeploymentConfiguration)

And set its value in each one to either Debug or Release

并将其值分别设置为Debug或Release

Then in any of your Pre or Post deployment scripts you can do something like so:

然后,在任何部署前或部署后脚本中,您都可以这样做:

IF '$(DeploymentConfiguration)' = 'Debug'
BEGIN
PRINT 'Executing Debug deployment'
:r .\Debug\SomeNeededScript.sql
END

IF '$(DeploymentConfiguration)' = 'Release'
BEGIN
PRINT 'Executing Release deployment'
:r .\Release\Anotherscript.sql
END