1

我可以知道.Net 中是否有任何库可以让我传入 2 个连接字符串并进行架构比较和更新?

我需要这个的原因是因为我维护了一个黄金数据库,它为不同的客户部署到多个数据库。如果每次有数据库更新时我都需要从 VS 手动进行模式比较会消耗很长时间。

我计划遍历所有客户端连接字符串并与黄金数据库进行比较并自动更新它们。

请指教,谢谢。

4

2 回答 2

2

这就是我所做的。一个非常简单的解决方案。所需的 nuget 库如下所示,搜索并将它们包含在项目中。

<package id="Microsoft.SqlServer.Dac" version="1.0.3" targetFramework="net45" />
<package id="Microsoft.SqlServer.DacFx.x86" version="130.3485.1" targetFramework="net45" />

下面是示例代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.SqlServer.Dac.Compare;

    namespace SchemaComparer
    {
        class Program
        {
            //The directory where all the scmp files are located
            const string schemaDirectory = "C:\SchemaCompare\\";

            static void Main(string[] args)
            {
                //Loop thru all the scmp from the directory. This set to max 2 thread that run parallel and update together
                Parallel.ForEach(Directory.GetFiles(schemaDirectory), new ParallelOptions { MaxDegreeOfParallelism = 2 }, (file) =>   
                {
                    try
                    {
                        // Load comparison from Schema Compare (.scmp) file
                        var comparison = new SchemaComparison(file);

                        Console.WriteLine("Processing " + Path.GetFileName(file));
                        Console.WriteLine("Comparing schema...");

                        SchemaComparisonResult comparisonResult = comparison.Compare();

                        // Publish the changes to the target database
                        Console.WriteLine("Publishing schema...");

                        SchemaComparePublishResult publishResult = comparisonResult.PublishChangesToTarget();

                        Console.WriteLine(publishResult.Success ? "Publish succeeded." : "Publish failed.");
                        Console.WriteLine(" ");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                });

                Console.ReadLine();
            }
        }
    }

于 2017 年 11 月 3 日编辑 我可能忘记提及,为了使其正常工作,您必须创建 scmp 文件并将其存储在目录中,在我的情况下为“C:\SchemaCompare\”。该程序将拾取所有 scmp 文件并进行比较和更新。因此,如果您将来需要比较其他数据库,只需创建 scmp 并保存在目录中即可。

于 2017-04-24T01:45:15.043 回答
0

Devart 有一些支持命令行自动化的模式比较工具,并且可能能够满足您的需求:https ://www.devart.com/dbforge/sql/schemacompare/

于 2016-11-22T07:53:25.453 回答