C#自动架构比较SQL数据库和更新列表

问题描述 投票:0回答:2

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

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

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

请指教,谢谢。

c# sql database-schema schema-compare
2个回答
1
投票

这就是我所做的。一个非常简单的解决方需要的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并保留在目录中。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.