简体   繁体   English

我如何以编程方式发布SQL Server数据库项目?

[英]How can I programatically publish a SQL Server Database Project?

Consider the following situation: 考虑以下情况:

Now, I have a C# application that parses a file in order to get details (tables, columns etc) and starts a new SQL Connection in order to execute a SQL Command to create those tables in the database. 现在,我有一个C#应用程序解析文件以获取详细信息(表,列等)并启动新的SQL连接以执行SQL命令以在数据库中创建这些表。

What I want is to create a SQL Project in which I will manually create those tables, and from the C# application I want to programatically publish the SQL project to a certain server and database. 我想要的是创建一个SQL项目,我将手动创建这些表,并从C#应用程序,我想以编程方式将SQL项目发布到某个服务器和数据库。

Is this possible ? 这可能吗 ?

If you are using a sqlproj based project in .NET 4 and above, you can build and publish it programatically fairly easily using classes in the Microsoft.Build namespace. 如果您在.NET 4及更高版本中使用基于sqlproj的项目,则可以使用Microsoft.Build命名空间中的类以编程方式构建和发布它。 Taken from my answer here : 从我的答案采取在这里

using Microsoft.Build.Framework;
using Microsoft.Build.Execution;

public void UpdateSchema() {
    var props = new Dictionary<string, string> {
        { "UpdateDatabase", "True" },
        { "PublishScriptFileName", "schema-update.sql" },
        { "SqlPublishProfilePath", "path/to/publish.xml") }
    };

    var projPath = "path/to/database.sqlproj";

    var result = BuildManager.DefaultBuildManager.Build(
        new BuildParameters { Loggers = new[] { new ConsoleLogger() } },
        new BuildRequestData(new ProjectInstance(projPath, props, null), new[] { "Publish" }));

    if (result.OverallResult == BuildResultCode.Success) {
        Console.WriteLine("Schema update succeeded!");
    }
    else {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Schema update failed!");
        Console.ResetColor();
    }
}

private class ConsoleLogger : ILogger
{
    public void Initialize(IEventSource eventSource) {
        eventSource.ErrorRaised += (sender, e) => {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(e.Message);
            Console.ResetColor();
        };
        eventSource.MessageRaised += (sender, e) => {
            if (e.Importance != MessageImportance.Low)
                Console.WriteLine(e.Message);
        };
    }
    public void Shutdown() { }
    public LoggerVerbosity Verbosity { get; set; }
    public string Parameters { get; set; }
}

This is for .NET 4 and above. 这适用于.NET 4及更高版本。 Be sure and include assembly references to Microsoft.Build and Microsoft.Build.Framework . 确保并包含对Microsoft.BuildMicrosoft.Build.Framework的程序集引用。

There are a number of ways you could accomplish this. 有很多方法可以实现这一目标。 In our app (~ 7 large databases) we manage them all with Database Projects from SQL Server Data Tools . 在我们的应用程序(~7个大型数据库)中,我们使用SQL Server数据工具中的数据库项目管理它们。 This has allowed us to version control easily as well as do some awesome comparison tools at deploy time and a plethora of other options. 这使我们可以轻松地进行版本控制,并在部署时做一些很棒的比较工具以及许多其他选项。 We did expand ours to deal with some nuances in our environment but for most people that shouldn't be an issue. 我们确实扩展了我们的环境,以应对我们环境中的一些细微差别,但对于大多数人来说这应该不是问题。

Part of that toolset includes DAC (Data Tier Applications) which allow you to transplant a database that is in your project to various environments pretty easily. 该工具集的一部分包括DAC(数据层应用程序),它允许您将项目中的数据库移植到各种环境中。 This would support a great majority of projects in existence today. 这将支持当今存在的绝大多数项目。

If you wanted to go pure programatic you could use Code First (and Code First Migrations which is pretty slick) which is kind of build as you go and MS will figure out the rest for you (mainly by convention but flexibility to go beyond that). 如果你想要纯粹的程序设计,你可以使用Code First(和Code First Migrations ,这是非常光滑的),这是一种构建,你会去,MS将为你找出其余的(主要是按照惯例,但灵活性超越它) 。 It's really friendly when it comes to upgrading versions. 在升级版本时它非常友好。 Again IMHO. 恕我直言。

Database Projects exist as well but tend to require a little more insight/work to get them tweaked the way you want (but also offer a familar SQL Explorer type layout). 数据库项目也存在,但往往需要更多的洞察力/工作来按照您想要的方式进行调整(但也提供熟悉的SQL Explorer类型布局)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM