static void BuildDac(string dacpacPath, DacPacVersion dacpacVersion) { Console.WriteLine("\n------- Building DacPac -------"); if (mySQLScripts.Scripts.Count >= 0) { // Figure out which version we want to build. Microsoft.SqlServer.Dac.Model.SqlServerVersion modelVersion; switch (dacpacVersion) { case DacPacVersion.SQL2012: modelVersion = Microsoft.SqlServer.Dac.Model.SqlServerVersion.Sql110; break; case DacPacVersion.SQL2014: modelVersion = Microsoft.SqlServer.Dac.Model.SqlServerVersion.Sql120; break; case DacPacVersion.SQL2016: modelVersion = Microsoft.SqlServer.Dac.Model.SqlServerVersion.Sql130; break; default: modelVersion = Microsoft.SqlServer.Dac.Model.SqlServerVersion.Sql110; break; } using (TSqlModel model = new TSqlModel(modelVersion, new TSqlModelOptions { })) { foreach (string SQLScript in mySQLScripts.Scripts) { // Add it to the model model.AddObjects(SQLScript); } // Now write the model try { DacPackageExtensions.BuildPackage( dacpacPath, model, new PackageMetadata { Name = "Mini_DacPac", Description = "Built by DacMini.", Version = "1.0" }, new PackageOptions() ); Console.WriteLine("DAC package created."); } catch (Microsoft.SqlServer.Dac.DacServicesException e) { Console.WriteLine("DAC creation failed:"); Console.WriteLine("Error: " + e.Message); } catch (Exception e) { Console.WriteLine("DAC creation may have failed:"); Console.WriteLine("Error: " + e.Message); } } } else { Console.WriteLine("Warning - No scripts were found to package into DacPac."); } }
//private static StringCollection SQLScripts = new StringCollection(); // Store of all scripts retrived from file and SQL server. //private static StringCollection MissingSQLScripts = new StringCollection(); // List of missing script files, or SQL objects. static void Main(string[] args) { string DacpacOutputFilename = string.Empty; string DBListFilename = string.Empty; string SQLVersion = string.Empty; DacPacVersion DacPacVersion = DacPacVersion.SQL2016; CommandLineParser.Arguments CommandLine = new CommandLineParser.Arguments(args); if (CommandLine["dacpac"] != null) { DacpacOutputFilename = CommandLine["dacpac"]; } if (CommandLine["objectlist"] != null) { DBListFilename = CommandLine["objectlist"]; } if (CommandLine["verbose"] != null) { PrintVerboseInfo = true; } if (CommandLine["sqlversion"] != null) { SQLVersion = CommandLine["sqlversion"]; switch (SQLVersion.ToUpper()) { case "SQL2012": DacPacVersion = DacPacVersion.SQL2012; break; case "SQL2014": DacPacVersion = DacPacVersion.SQL2014; break; case "SQL2016": DacPacVersion = DacPacVersion.SQL2016; break; case "SQL2017": DacPacVersion = DacPacVersion.SQL2016; break; default: DacPacVersion = DacPacVersion.SQL2016; break; } } // If no Dacpac output file is specified, or both DBlist and FileList are // not specified then output usage info. if (DacpacOutputFilename == String.Empty || DBListFilename == String.Empty) { PrintUsage(); } else { // Check the Dacoutput path DacpacOutputFilename = CheckPathDirectory(DacpacOutputFilename); // Collect SQL Scripts from DB if (DBListFilename != String.Empty) { DBListFilename = CheckPathDirectory(DBListFilename); GetDBScripts(DBListFilename); } // Show scripts we were unable to read or retrieve if there are any if (mySQLScripts.MissingScripts.Count > 0) { Console.WriteLine("\n***** Script reading errors and warnings:"); foreach (string MissingObject in mySQLScripts.MissingScripts) { Console.WriteLine("*** " + MissingObject); } } // Build DacPac BuildDac(DacpacOutputFilename, DacPacVersion); // End Console.WriteLine("\nProcess Complete."); } }