Using Microsoft.Build.Evaluation to publish a database project (.sqlproj)

后端 未结 4 2020
耶瑟儿~
耶瑟儿~ 2020-12-09 04:57

I need to be able to publish an SSDT project programmatically. I am looking at using Microsoft.Build to do so but can not find any documentation. It seems pretty simple to c

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 05:33

    I had to do something similar to this because VSDBCMD which we previously used does not deploy to SQL Server 2012 and we needed to support it. What I found was the Microsoft.SqlServer.Dac assembly which seems to come as part of the SQL Server data tools (http://msdn.microsoft.com/en-us/data/tools.aspx)

    When you run this on the client machine you will need the full version of the .NET 4 framework and the SQL CLR types and SQL T-SQL ScriptDOM pack found here: http://www.microsoft.com/en-us/download/details.aspx?id=29065

    Code below is from a mockup I made for testing the new deployment method and deploys a given .dacpac file

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using Microsoft.SqlServer.Dac;
        using System.IO;
    
        namespace ConsoleApplication3
        {
            class Program
            {
                private static TextWriter output = new StreamWriter("output.txt", false);
                static void Main(string[] args)
                {
    
                    Console.Write("Connection String:");
                    //Class responsible for the deployment. (Connection string supplied by console input for now)
                    DacServices dbServices = new DacServices(Console.ReadLine());
    
                    //Wire up events for Deploy messages and for task progress (For less verbose output, don't subscribe to Message Event (handy for debugging perhaps?)
                    dbServices.Message += new EventHandler(dbServices_Message);
                    dbServices.ProgressChanged += new EventHandler(dbServices_ProgressChanged);
    
    
                    //This Snapshot should be created by our build process using MSDeploy
                    Console.WriteLine("Snapshot Path:");
    
                    DacPackage dbPackage = DacPackage.Load(Console.ReadLine());
    
    
    
    
                    DacDeployOptions dbDeployOptions = new DacDeployOptions();
                    //Cut out a lot of options here for configuring deployment, but are all part of DacDeployOptions
                    dbDeployOptions.SqlCommandVariableValues.Add("debug", "false");
    
    
                    dbServices.Deploy(dbPackage, "trunk", true, dbDeployOptions);
                    output.Close();
    
                }
    
                static void dbServices_Message(object sender, DacMessageEventArgs e)
                {
                    output.WriteLine("DAC Message: {0}", e.Message);
                }
    
                static void dbServices_ProgressChanged(object sender, DacProgressEventArgs e)
                {
                    output.WriteLine(e.Status + ": " + e.Message);
                }
            }
        }
    

    This seems to work on all versions of SQL Server from 2005 and up. There is a similar set of objects available in Microsoft.SqlServer.Management.Dac, however I believe this is in the previous version of DACFx and is not included in the latest version. So use the latest version if you can.

提交回复
热议问题