使用Microsoft.Build.Evaluation发布数据库项目(.sqlproj) [英] Using Microsoft.Build.Evaluation to publish a database project (.sqlproj)
问题描述
我需要能够以编程方式发布SSDT项目。我期待在使用Microsoft.Build这样做,但无法找到任何文件。这似乎很简单,以创建.dacpac,但我怎么会或者发布到一个现有的数据库,或者至少是一个.sql文件。这个想法是有它做它,当我在项目点击右键并选择发布。它应该有选择的数据库进行比较,并生成一个升级脚本
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 create the .dacpac, but how would I either publish to an existing database or at the very least to a .sql file. The idea is to have it do what it does when I right click on the project and select publish. It should compare with a selected database and generate an upgrade script.
这是我迄今为止创建.dacpac:
This is what I have so far to create the .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实例节目,但在代码
Essentially I am trying to do what this MSBuild Example shows but in code.
对不起,这是我的一切。在构建类的doecumentation很差。任何帮助,将不胜感激。
Sorry that this is all I have. The doecumentation on the Build classes is very poor. Any help would be appreciated.
感谢。
推荐答案
我有做类似这样的东西,因为这是我们以前使用的VSDBCMD不会部署到SQL Server 2012,我们需要支持它。我发现是Microsoft.SqlServer.Dac组件,其似乎来作为SQL Server数据工具的一部分( http://msdn.microsoft.com/en-us/data/tools.aspx )
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)
当您在客户机上运行这个你需要完整版本的.NET 4框架和SQL CLR类型和SQL T-SQL ScriptDOM包这里找到的 http://www.microsoft.com/en-us/download/details.aspx?id=29065
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
下面的代码是一个样机我用于测试新的部署方法制成,部署一个给定的.dacpac文件
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<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的,不包含在最新的版本。如果你能这样使用最新版本。
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.
这篇关于使用Microsoft.Build.Evaluation发布数据库项目(.sqlproj)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!