7

我刚刚将现有的 SQL Server 2008 r2 .dbproj 升级到了 SQL Server 2012 .sqlproj(使用 SQL Server Data Tools)。

以前,我能够在我的项目中定义一个 SQLCMD 变量,然后通过编辑项目文件来定义值,以通过添加以下元素来使用 msbuild 值:

<ItemGroup>
    <SqlCommandVariableOverride Include="ProjectDirectory=$(MSBuildProjectDirectory)" />
</ItemGroup>

然后我可以在我的 PostDeployment 脚本中使用它,如下所示:

SELECT * INTO dbo.MyTable FROM dbo.MyTable WHERE 1=2
BULK INSERT dbo.MyTable
FROM  '$(ProjectDirectory)\data\dbo.MyTable.dat'
WITH (DATAFILETYPE = 'widenative')

但是,升级后,这似乎不再起作用。

我已经尝试将相同的条目添加到新的 sqlproj,但发布功能似乎没有选择它并希望我提供一个值。如果我提供$(MSBuildProjectDirectory),则按字面意思解释并失败。

在新制度下,指定本地文件路径和/或使用 msbuild 值的机制是什么?

4

1 回答 1

3

In a sql server 2012 sqlproj (SSDT database project) you use publishing profiles. You can start off by right-clicking your database project and choosing 'Publish'.

You can then set desired options and save these in a so-called publishing profile in your project. Double-clicking this profile launches the publishing wizard with the correct options set.

In your publish profile you can include hard-coded values for sqlcmd variables:

<ItemGroup>
    <SqlCmdVariable Include="ProjectDirectory">
        <Value>UNKNOWN</Value>
    </SqlCmdVariable>
</ItemGroup>

If desired, you can update these with dynamic values during build. In your msbuild project:

<Target Name="SetProjectDirectoryInPublishXml">
    <ItemGroup>
        <Namespaces Include="nsMsbuild">
            <Prefix>nsMsbuild</Prefix>
            <Uri>http://schemas.microsoft.com/developer/msbuild/2003</Uri>
        </Namespaces>
    </ItemGroup>
    <ItemGroup>
        <SSDTPublishFiles Include="$(SolutionBinFolder)\**\*.publish.xml" />
    </ItemGroup>
    <MSBuild.ExtensionPack.Xml.XmlFile Condition="%(SSDTPublishFiles.Identity) != ''"
                                   TaskAction="UpdateElement"
                                   File="%(SSDTPublishFiles.Identity)"
                                   Namespaces="@(Namespaces)" 
                                   XPath="//nsMsbuild:SqlCmdVariable[@Include='ProjectDirectory']/nsMsbuild:Value" 
                                   InnerText="$(MSBuildProjectDirectory)"/>
</Target>

This requires an extension to update the XML. I use the msbuild extension pack.

Credits for this mechanism go to Jamie Thomson

于 2013-10-16T06:42:17.233 回答