public void ScmFileBasedCompare(string srcConString, string targetConString)
        {
            var src = new SqlConnectionStringBuilder(srcConString);

            var bacPacFile = GetDacFileName(srcConString) + ".dacpac";
            var dtVersion  = DateTime.Now.ToString("yyyy.MM.dd.HHmmss");

            //if Bac file not exists create new
            var dacService = new Microsoft.SqlServer.Dac.DacServices(srcConString);

            dacService.Extract(bacPacFile, src.InitialCatalog, "Test Application", Version.Parse(dtVersion));

            var sourceDacpac = new SchemaCompareDacpacEndpoint(bacPacFile);

            var target         = new SqlConnectionStringBuilder(targetConString);
            var targetDatabase = new SchemaCompareDatabaseEndpoint(targetConString);

            var comparison = new SchemaComparison(sourceDacpac, targetDatabase);

            // Persist comparison file to disk in Schema Compare (.scmp) format
            comparison.SaveToFile(@"C:\temp\mycomparison.scmp");

            // Load comparison from Schema Compare (.scmp) file
            comparison = new SchemaComparison(@"C:\temp\mycomparison.scmp");
            SchemaComparisonResult comparisonResult = comparison.Compare();

            foreach (var d in comparisonResult.Differences)
            {
                Console.WriteLine(d.SourceObject.GetScript());
            }



            // Find the change to table1 and exclude it.
            //foreach (SchemaDifference difference in comparisonResult.Differences)
            //{
            //    if (difference.TargetObject.Name != null &&
            //        difference.TargetObject.Name.HasName &&
            //        difference.TargetObject.Name.Parts[1] == "DbConnections")
            //    {
            //        comparisonResult.Exclude(difference);
            //        break;
            //    }
            //}


            // Publish the changes to the target database
            //SchemaComparePublishResult publishResult = comparisonResult.PublishChangesToTarget();
            var publishResult = comparisonResult.GenerateScript(".");


            Console.WriteLine(publishResult.MasterScript);
            Console.WriteLine(publishResult.Script);
            Console.WriteLine(publishResult.Success ? "Publish succeeded." : "Publish failed.");
        }
Ejemplo n.º 2
0
        public void ExtractDAC(string destServer, string databaseName, Version version, string extractFolderPath)

        {
            try
            {
                //  Console.WriteLine(destServer.Information.Version);
                Microsoft.SqlServer.Dac.DacServices dacUnit = new Microsoft.SqlServer.Dac.DacServices(destServer);

                // DacExtractionUnit dacUnit = new DacExtractionUnit(destServer, databaseName, databaseName, new Version(version));



                DirectoryInfo dir = new DirectoryInfo(extractFolderPath);

                if (!dir.Exists)
                {
                    dir.Create();
                }

                string dacFilePath = dir.FullName + @"\" + databaseName + ".dacpac";

                if (File.Exists(dacFilePath))
                {
                    File.Delete(dacFilePath);
                }
                dacUnit.Extract(dacFilePath, databaseName, databaseName, version);
                filem = dacFilePath;
                //  dacUnit.ExportBacpac(dacFilePath, databaseName);
                // dacUnit.Extract(dacFilePath);

                // Console.WriteLine("extract " + databaseName + "successfully");
            }

            catch (IOException)
            {
                Console.WriteLine("please check extract folder");

                Console.WriteLine("extract " + databaseName + "failed");
            }

            catch (Exception EXC)
            {
                Console.WriteLine("extract " + databaseName + "failed");
            }
        }