我需要能够以编程方式发布 SSDT 项目。我正在考虑使用 Microsoft.Build 这样做,但找不到任何文档。创建 .dacpac 似乎很简单,但我将如何发布到现有数据库或至少发布到 .sql 文件。我的想法是让它做当我右键单击项目并选择发布时它所做的事情。它应该与选定的数据库进行比较并生成升级脚本。
这是我到目前为止创建 .dacpac 的方法:
partial class DBDeploy
{
Project project;
internal void publishChanges()
{
Console.WriteLine("Building project " + ProjectPath);
Stopwatch sw = new Stopwatch();
sw.Start();
project = ProjectCollection.GlobalProjectCollection.LoadProject(ProjectPath);
project.Build();
//at this point the .dacpac is built and put in the debug folder for the project
sw.Stop();
Console.WriteLine("Project build Complete. Total time: {0}", sw.Elapsed.ToString());
}
}
本质上我正在尝试做这个 MSBuild Example显示但在代码中。
抱歉,我只有这些。 Build 类的文档非常差。任何帮助将不胜感激。
谢谢。
最佳答案
我不得不做类似的事情,因为我们之前使用的 VSDBCMD 没有部署到 SQL Server 2012,我们需要支持它。我发现的是 Microsoft.SqlServer.Dac 程序集,它似乎是 SQL Server 数据工具的一部分 (http://msdn.microsoft.com/en-us/data/tools.aspx)
当您在客户端计算机上运行此程序时,您将需要完整版本的 .NET 4 框架和 SQL CLR 类型以及可在此处找到的 SQL T-SQL ScriptDOM 包:http://www.microsoft.com/en-us/download/details.aspx?id=29065
下面的代码来 self 为测试新部署方法和部署给定的 .dacpac 文件而制作的模型
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<DacMessageEventArgs>(dbServices_Message);
dbServices.ProgressChanged += new EventHandler<DacProgressEventArgs>(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);
}
}
}
这似乎适用于 2005 及更高版本的 SQL Server。 Microsoft.SqlServer.Management.Dac 中有一组类似的对象,但我相信这是在以前版本的 DACFx 中,未包含在最新版本中。因此,如果可以,请使用最新版本。
关于c# - 使用 Microsoft.Build.Evaluation 发布数据库项目 (.sqlproj),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10438258/