我需要能够以编程方式发布SSDT项目。我正在考虑使用Microsoft.Build来做到这一点,但找不到任何文档。创建.dacpac似乎相当简单,但是如何发布到现有数据库或至少发布到.sql文件呢?我的想法是,当我右键单击项目并选择publish时,让它做它所做的事情。它应该与选定的数据库进行比较,并生成升级脚本。
这是我到目前为止用来创建.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类的文档描述非常糟糕。任何帮助都将不胜感激。
谢谢。
发布于 2012-08-13 20:03:54
我不得不做一些类似的事情,因为我们之前使用的VSDBCMD没有部署到SQL Server 2012,我们需要支持它。我发现了Microsoft.SqlServer.Dac程序集,它似乎是SQL Server data tools (http://msdn.microsoft.com/en-us/data/tools.aspx)的一部分。
在客户端计算机上运行此命令时,您将需要.NET 4框架的完整版以及位于以下位置的SQL类型和SQL SQL包:http://www.microsoft.com/en-us/download/details.aspx?id=29065
下面的代码来自我为测试新部署方法而制作的样机,并部署了一个给定的.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的前一个版本中提供的,并且没有包含在最新的版本中。因此,如果可以,请使用最新版本。
发布于 2012-06-07 22:04:33
我们需要一种方法来告诉msbuild如何发布以及在哪里发布。在Visual Studio中打开您的项目并开始Publish它。在对话框中输入所有需要的信息,包括数据库连接信息和任何自定义SQLCMD变量值。指向文件的Save Profile As...,例如Northwind.publish.xml。(然后您可以使用Cancel。)现在我们可以使用这个文件和项目文件来构建和发布:
// Create a logger.
FileLogger logger = new FileLogger();
logger.Parameters = @"logfile=Northwind.msbuild.log";
// Set up properties.
var projects = ProjectCollection.GlobalProjectCollection;
projects.SetGlobalProperty("Configuration", "Debug");
projects.SetGlobalProperty("SqlPublishProfilePath", @"Northwind.publish.xml");
// Load and build project.
var dbProject = ProjectCollection.GlobalProjectCollection.LoadProject(@"Northwind.sqlproj");
dbProject.Build(new[]{"Build", "Publish"}, new[]{logger});这可能需要一段时间,并且可能看起来卡住了。耐心点。:)
发布于 2012-06-12 02:48:46
您应该使用SqlPackage.exe发布您的dacpac。
SqlPackage.exe
/Action:Publish
/SourceFile:C:/file.dacpac
/TargetConnectionString:[Connection string]此外,您可以将设置保存到DAC发布配置文件中,而不是传递太多参数(这可以在visual studio中完成)。
https://stackoverflow.com/questions/10438258
复制相似问题