static void Main(string[] args) { try { Parameters parameters; if (!Parameters.TryParse(args, out parameters)) { return; } var outputDirectory = parameters.OutputDirectory; var connectionString = BuildConnectionString(parameters); using (var sc = new SqlConnection(connectionString)) { if (outputDirectory != null && (outputDirectory.Contains("{server}") || outputDirectory.Contains("{serverclean}"))) { var serverConnection = new ServerConnection(sc); var s = new Server(serverConnection); // Get the proper case for the database name //var database = s.Databases[sc.Database.ToLowerInvariant()].Name; // If I index into the collection, the .Name property gives me back the same case I put in! var database = s.Databases.Cast<Database>().Single(d => d.Name.ToUpperInvariant() == sc.Database.ToUpperInvariant()).Name; outputDirectory = outputDirectory.Replace("{server}", s.Name); outputDirectory = outputDirectory.Replace("{serverclean}", DatabaseScripter.FixUpFileName(s.Name)); outputDirectory = outputDirectory.Replace("{database}", database); outputDirectory = outputDirectory.Replace("{databaseclean}", DatabaseScripter.FixUpFileName(database)); } } if (outputDirectory != null && !Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } var ds = new DatabaseScripter(); ds.ConnectionString = connectionString; ds.TableFilter = parameters.TableFilter.ToArray(); ds.TableDataFilter = parameters.TableDataFilter.ToArray(); var tableDataFile = parameters.TableDataFilterFile; if(tableDataFile != null) { ds.DatabaseTableDataFilter = ReadTableDataFile(tableDataFile); } ds.ViewsFilter = parameters.ViewFilter.ToArray(); ds.SprocsFilter = parameters.StoredProcedureFilter.ToArray(); ds.UdfsFilter = parameters.UserDefinedFunctionFilter.ToArray(); ds.TableOneFile = parameters.TableOneFile; ds.ScriptAsCreate = parameters.ScriptAsCreate; ds.Permissions = parameters.ScriptPermissions; ds.NoCollation = parameters.NoCollation; ds.Statistics = parameters.ScriptStatistics; ds.IncludeDatabase = parameters.ScriptDatabase; ds.IncludeSystemObjects = parameters.IncludeSystemObjects; ds.CreateOnly = parameters.ScriptCreateOnly; ds.OutputFileName = parameters.OutputFileName; ds.StartCommand = parameters.StartCommand; ds.PreScriptingCommand = parameters.PreScriptingCommand; ds.PostScriptingCommand = parameters.PostScriptingCommand; ds.FinishCommand = parameters.FinishCommand; var watch = new Stopwatch(); watch.Start(); ds.GenerateScripts(outputDirectory, parameters.ScriptAllDatabases, parameters.Purge, parameters.DataScriptingFormat, parameters.Verbose, parameters.ScriptProperties); Console.Error.WriteLine("Took {0} ms", watch.ElapsedMilliseconds); } catch (Exception e) { Console.Error.WriteLine("Exception caught in Main()"); Console.Error.WriteLine("---------------------------------------"); while (e != null) { Console.Error.WriteLine(e.Message); Console.Error.WriteLine(); Console.Error.WriteLine(e.GetType().FullName); Console.Error.WriteLine(); Console.Error.WriteLine(e.StackTrace); Console.Error.WriteLine("---------------------------------------"); e = e.InnerException; } } }
static void Main(string[] args) { Utility.Arguments arguments = new Arguments(args); try { if (arguments["?"] != null) { PrintHelp(); return; } string connStr = arguments["con"]; string outputDirectory = arguments["outDir"]; bool scriptData = arguments["d"] != null; bool verbose = arguments["v"] != null; bool scriptProperties = arguments["p"] != null; bool Purge = arguments["Purge"] != null; bool scriptAllDatabases = arguments["ScriptAllDatabases"] != null; if (connStr == null || outputDirectory == null) { PrintHelp(); return; } string database = null; using (SqlConnection sc = new SqlConnection(connStr)) { database = sc.Database; } // Purge at the Server level only when we're doing all databases if (Purge && scriptAllDatabases && Directory.Exists(outputDirectory)) { PurgeDirectory(outputDirectory, "*.sql"); } if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } DatabaseScripter ds = new DatabaseScripter(); if (arguments["table"] != null) { ds.TableFilter = arguments["table"].Split(','); } if (arguments["view"] != null) { ds.ViewsFilter = arguments["view"].Split(','); } if (arguments["sp"] != null) { ds.SprocsFilter = arguments["sp"].Split(','); } if (arguments["TableOneFile"] != null) { ds.TableOneFile = true; } if (arguments["ScriptAsCreate"] != null) { ds.ScriptAsCreate = true; } if (arguments["Permissions"] != null) { ds.Permissions = true; } if (arguments["NoCollation"] != null) { ds.NoCollation = true; } if (arguments["IncludeDatabase"] != null) { ds.IncludeDatabase = true; } if (arguments["CreateOnly"] != null) { ds.CreateOnly = true; } if (arguments["filename"] != null) { ds.OutputFileName = arguments["filename"]; } ds.GenerateScripts(connStr, outputDirectory, scriptAllDatabases, Purge, scriptData, verbose, scriptProperties); } catch (Exception e) { Console.WriteLine("Exception caught in Main()"); Console.WriteLine("---------------------------------------"); while (e != null) { Console.WriteLine(e.Message); Console.WriteLine(); Console.WriteLine(e.GetType().FullName); Console.WriteLine(); Console.WriteLine(e.StackTrace); Console.WriteLine("---------------------------------------"); e = e.InnerException; } } }
static void Main(string[] args) { Utility.Arguments arguments = new Arguments(args); try { if (arguments["?"] != null) { PrintHelp(); return; } string connStr = arguments["con"]; string outputDirectory = arguments["outDir"]; bool scriptData = arguments["d"] != null; bool verbose = arguments["v"] != null; bool scriptProperties = arguments["p"] != null; bool Purge = arguments["Purge"] != null; bool scriptAllDatabases = arguments["ScriptAllDatabases"] != null; if (connStr == null || outputDirectory == null) { PrintHelp(); return; } string database = null; using (SqlConnection sc = new SqlConnection(connStr)) { database = sc.Database; } // Purge at the Server level only when we're doing all databases if (Purge && scriptAllDatabases && Directory.Exists(outputDirectory)) PurgeDirectory(outputDirectory, "*.sql"); if (!Directory.Exists(outputDirectory)) Directory.CreateDirectory(outputDirectory); DatabaseScripter ds = new DatabaseScripter(); if (arguments["table"] != null) ds.TableFilter = arguments["table"].Split(','); if (arguments["view"] != null) ds.ViewsFilter = arguments["view"].Split(','); if (arguments["sp"] != null) ds.SprocsFilter = arguments["sp"].Split(','); if (arguments["TableOneFile"] != null) ds.TableOneFile = true; if (arguments["ScriptAsCreate"] != null) ds.ScriptAsCreate = true; if (arguments["Permissions"] != null) ds.Permissions = true; if (arguments["NoCollation"] != null) ds.NoCollation = true; if (arguments["IncludeDatabase"] != null) ds.IncludeDatabase = true; if (arguments["CreateOnly"] != null) ds.CreateOnly = true; if (arguments["filename"] != null) ds.OutputFileName = arguments["filename"]; ds.GenerateScripts(connStr, outputDirectory, scriptAllDatabases, Purge, scriptData, verbose, scriptProperties); } catch (Exception e) { Console.WriteLine("Exception caught in Main()"); Console.WriteLine("---------------------------------------"); while (e != null) { Console.WriteLine(e.Message); Console.WriteLine(); Console.WriteLine(e.GetType().FullName); Console.WriteLine(); Console.WriteLine(e.StackTrace); Console.WriteLine("---------------------------------------"); e = e.InnerException; } } }