Advertisement
Udgin

Untitled

May 28th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.15 KB | None | 0 0
  1. private static void Main(string[] args)
  2.         {
  3.             var arguments = args.Select(x => x.ToLower()).ToList();
  4.             if (arguments.Count == 0 || arguments.Contains("-help"))
  5.             {
  6.                 Console.WriteLine("-d - Database Name");
  7.                 Console.WriteLine("-i - Output Sql File Name");
  8.                 Console.WriteLine("-s - Server Instance");
  9.                 Console.WriteLine("-u - User Name");
  10.                 Console.WriteLine("-p - Password");
  11.                 Console.WriteLine("It should be 'tables.txt' file in folder with names of tables to script; if it does not exist the application scripts all tables with prefix 'GMP_'");
  12.                 Console.ReadKey();
  13.             }
  14.             else if (arguments.Count > 1 && arguments.Contains("-d") && args.Contains("-i"))
  15.             {
  16.                 if (arguments.Count <= arguments.IndexOf("-d") + 1)
  17.                 {
  18.                     throw new ArgumentException("Database Name");
  19.                 }
  20.  
  21.                 if (arguments.Count <= arguments.IndexOf("-i") + 1)
  22.                 {
  23.                     throw new ArgumentException("Output Sql File Name");
  24.                 }
  25.  
  26.                 var dbName = arguments[arguments.IndexOf("-d") + 1];
  27.                 var outputFileName = arguments[arguments.IndexOf("-i") + 1];
  28.                 var srv = new Server();
  29.                 if (arguments.Contains("-s") && args.Contains("-u") && arguments.Contains("-p"))
  30.                 {
  31.                     if (arguments.Count <= arguments.IndexOf("-s") + 1)
  32.                     {
  33.                         throw new ArgumentException("Server Instance");
  34.                     }
  35.                     if (arguments.Count <= arguments.IndexOf("-u") + 1)
  36.                     {
  37.                         throw new ArgumentException("User Name");
  38.                     }
  39.                     if (arguments.Count <= arguments.IndexOf("-p") + 1)
  40.                     {
  41.                         throw new ArgumentException("Password");
  42.                     }
  43.                     var connection = new ServerConnection(arguments[arguments.IndexOf("-s") + 1], arguments[arguments.IndexOf("-u") + 1], arguments[arguments.IndexOf("-p") + 1]);
  44.                     srv = new Server(connection);
  45.                 }
  46.  
  47.                 // read names of tables
  48.                 var tablesFromFile = new List<string>();
  49.                 if (File.Exists("tables.txt"))
  50.                 {
  51.                     using (var file = File.OpenText("tables.txt"))
  52.                     {
  53.                         while (file.Peek() > 0)
  54.                         {
  55.                             tablesFromFile.Add(file.ReadLine());
  56.                         }
  57.                     }
  58.                 }
  59.  
  60.                 Database db = srv.Databases[dbName];
  61.  
  62.                 var dropKeys = new Scripter(srv) {Options = {ScriptDrops = true, IncludeIfNotExists = true, DriForeignKeys = true}};
  63.                 var listOfScripts = new List<Scripter>
  64.                                     {
  65.                                         new Scripter(srv) {Options = {ScriptDrops = true, IncludeIfNotExists = true, DriAllKeys = false}},
  66.                                         new Scripter(srv) {Options = {ScriptDrops = false, ScriptSchema = true, WithDependencies = false, DriIndexes = true, DriClustered = true, IncludeIfNotExists = true, DriAllKeys = false}},
  67.                                         new Scripter(srv) {Options = {ScriptDrops = false, ScriptSchema = true, DriDefaults = false, DriIndexes = false, DriPrimaryKey = false, DriClustered = false, Default = false, DriAll = false, DriForeignKeys = true, IncludeIfNotExists = true, DriAllKeys = false}},
  68.                                         new Scripter(srv) {Options = {DriIndexes = true, Default = true, DriDefaults = true, DriClustered = false, IncludeIfNotExists = true, DriAll = true, DriAllConstraints = true, DriAllKeys = true, SchemaQualify = true, SchemaQualifyForeignKeysReferences = true, NoCollation = true}}
  69.                                     };
  70.  
  71.                 using (var file = File.CreateText(outputFileName))
  72.                 {
  73.                     foreach (Table tb in db.Tables)
  74.                     {
  75.                         if ((tablesFromFile.Count > 0 && tablesFromFile.Contains(tb.Name) || (tablesFromFile.Count == 0 && tb.Name.StartsWith("GMP_"))))
  76.                         {
  77.                             if (tb.IsSystemObject == false)
  78.                             {
  79.                                 foreach (ForeignKey foreignKey in tb.ForeignKeys)
  80.                                 {
  81.                                     System.Collections.Specialized.StringCollection scd = dropKeys.Script(new[] { foreignKey.Urn });
  82.                                     foreach (string st in scd)
  83.                                     {
  84.                                         file.WriteLine(st);
  85.                                         file.WriteLine("GO");
  86.                                     }
  87.                                 }
  88.                                 file.WriteLine();
  89.                             }
  90.                         }
  91.                     }
  92.  
  93.                     foreach (var script in listOfScripts)
  94.                     {
  95.                         foreach (Table tb in db.Tables)
  96.                         {
  97.                             if ((tablesFromFile.Count > 0 && tablesFromFile.Contains(tb.Name) || (tablesFromFile.Count == 0 && tb.Name.StartsWith("GMP_"))))
  98.                             {
  99.                                 if (tb.IsSystemObject == false)
  100.                                 {
  101.                                     System.Collections.Specialized.StringCollection scd = script.Script(new[] { tb.Urn });
  102.                                     foreach (string st in scd)
  103.                                     {
  104.                                         file.WriteLine(st);
  105.                                         file.WriteLine("GO");
  106.                                     }
  107.                                     file.WriteLine();
  108.                                 }
  109.                             }
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement