Save Version Number to Database After Publish

71 Views Asked by At

We have WPF Application that publishes via ClickOnce.

I need to save ClickOnce Version Number (that auto increments) to a database after each successful publish.

Is there a event like OnPublishSucces that can trigger database operations after publishing ?

1

There are 1 best solutions below

0
On

I struggled with this one for a few hours and eventually patched together an answer from a few different sources (Definitely not all my own doing). This code goes just before the end tag of your csproj file. You can't alter the after publish events via the IDE. So the Target Name="AfterPublish" triggers only after the publish event and not the build. The code below grabs the applications version. The command utilises the SQLCMD.exe application to execute the queries.

<Target Name="AfterPublish">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="Targets" />
    </GetAssemblyIdentity>
    <ItemGroup>
      <VersionNumber Include="@(Targets->'%(Version)')" />
    </ItemGroup>
    <Exec Command="&quot;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.exe&quot;  -S SERVERNAME -U USERNAME -P PASSWORD -d DATABASENAME -Q &quot;UPDATE [TABLENAME] SET [COLUMNNAME] = '@(VersionNumber)'&quot;" />
  </Target>

Obviously replace the capital letter parameters with your own parameters and use " where ever double quotes are required within the command quotes (In this case around the file path for SQLCMD and the query itself.

Hope that helps.