12

使用 VS2010 数据库项目,我可以定义目标(或不)数据库,设置操作(仅创建脚本或创建脚本并部署到目标),执行“部署”(通过 VS 或 MSBuild)并获得结果脚本。

我从未使用过“创建和部署”操作,因为我只使用生成的脚本来构建安装程序,并且稍后会在安装过程中应用该脚本。

此功能允许我创建升级(仅自上一个版本以来的更改)或完整安装脚本(如果没有指向目标数据库)。

通常在使用 VS 2012 或 SSDT 时,我似乎找不到正确的选项组合来重现此“仅脚本”行为以进行升级和完整安装。

我发现这个问题涵盖了如何单击 VS 中的按钮,但它没有解决如何在 MSBuild 中完成此操作。

有人可以为我指出这个特定配置的有用资源吗?

4

2 回答 2

22

After hacking at it for several hours, I was able to get something workable. It comes down to two significant elements: getting a correct publish xml, and the correct arguments for MSBuild.

The publish xml file was the standard format that was created with VS. It looks like something this...

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <IncludeCompositeObjects>True</IncludeCompositeObjects>
    <TargetDatabaseName>BLANK</TargetDatabaseName>
    <DeployScriptFileName>DeployTarget.sql</DeployScriptFileName>
    <TargetConnectionString>Data Source=SERVER;Integrated Security=True;Pooling=False</TargetConnectionString>
    <ProfileVersionNumber>1</ProfileVersionNumber>
    <ScriptDatabaseOptions>False</ScriptDatabaseOptions>
    <BlockOnPossibleDataLoss>True</BlockOnPossibleDataLoss>
    <CommentOutSetVarDeclarations>False</CommentOutSetVarDeclarations>
  </PropertyGroup>
</Project>

With the previous project format, you could leave Target DB and connection string blank and it would generate a "complete" deployment script. However in SSDT sqlproj files, you must provide something, even if its incorrect, hence the "BLANK" database name. If I change this to an actual database, it would produce a delta script.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /p:Configuration=Release;Platform=AnyCPU;SqlPublishProfilePath="<fullPathToPublishXML";UpdateDatabase=False /t:Rebuild,Publish "<fullPathToSqlProj>"

This still does not honor the DeployScriptFileName that is in the publish xml (It creates a sql file with the same name as the publish profile), but does appear to create the correct content in the file.

于 2012-10-11T14:20:59.270 回答
7

你在正确的轨道上,但你缺少一个参数。如果要提供脚本名称,则应在 msbuild 执行中使用以下参数:

/p:PublishScriptFileName=[your output script.sql]
于 2013-04-09T12:24:20.867 回答