static void Main(string[] args)
 {
     try
       {
      AppArgs parsedArgs = new AppArgs();
      if (Utilities.Utility.ParseCommandLineArguments(args, parsedArgs))
      {
         App app = new App();
         app.Run(ref parsedArgs);
      }
      else
      {
         Console.Write(Utilities.Utility.CommandLineArgumentsUsage(typeof(AppArgs)));
         Console.WriteLine("<Command> = ClearPartition | CreateStagingFull | CreateStagingNoindex | CreateStagingClusteredIndex | IndexStaging");
         Environment.Exit(1);
      }
       }
       catch (Exception ex)
       {
      Exception thisEx = ex;
      while (thisEx != null)
      // Recursively print exception stack
      {
         Console.WriteLine(thisEx.Message, thisEx.Source);
         thisEx = thisEx.InnerException;
      }
      Environment.Exit(2);
       }
       Environment.Exit(0);
 }
        void Run(ref AppArgs parsedArgs)
        {
            ServerConnection sc = new ServerConnection();


            try
            {
                sc.ServerInstance   = parsedArgs.Server;
                sc.LoginSecure      = parsedArgs.Integrated;
                sc.StatementTimeout = 0;

                if (!parsedArgs.Integrated)
                {
                    sc.Login    = parsedArgs.User;
                    sc.Password = parsedArgs.Password;
                }
                sc.Connect();

                String command          = parsedArgs.Command.ToLower();
                String dbName           = parsedArgs.Database;
                String schName          = parsedArgs.Schema;
                String partitionTblName = parsedArgs.PartitionTable;
                String scriptOption     = (parsedArgs.ScriptOption == null) ? null : parsedArgs.ScriptOption.ToLower();

                // Set flag indicating whether scripts will be generated
                bool generateScript = (scriptOption == "i" || scriptOption == "o") ? true : false;
                // set flag indicated whether commands will be executed on SQL Server (as opposed to just scripting)
                bool executeCommands = (scriptOption == "o") ? false : true;


                // Ensure that either a partition number or range value is provided, not both
                int    partitionNumber     = parsedArgs.PartitionNumber;
                string partitionRangeValue = parsedArgs.PartitionRangeValue;
                if (((partitionRangeValue == null) && (partitionNumber == 0)) ||
                    ((partitionRangeValue != null) && (partitionNumber != 0)))
                {
                    throw new System.ArgumentException("Specify either a partition number OR a partition range value");
                }

                // Construct a staging table name likely to be unique, if no name is provided
                String stagingTblName = parsedArgs.StagingTable;
                if (stagingTblName == null)
                {
                    stagingTblName = partitionTblName + "_part" + partitionNumber.ToString("####", System.Globalization.CultureInfo.InvariantCulture)
                                     + partitionRangeValue + "_" + System.DateTime.Now.Ticks.ToString(System.Globalization.CultureInfo.InvariantCulture);
                }

                bool             keepStaging = parsedArgs.Keep;
                PartitionManager pm          = null;

                System.IO.StreamWriter scriptWriter = null;

                if ((scriptOption == "i" || scriptOption == "o") &&
                    parsedArgs.ScriptFile != null)
                {
                    try
                    {
                        scriptWriter = new System.IO.StreamWriter(parsedArgs.ScriptFile, true, Encoding.Unicode);
                    }
                    catch (System.IO.IOException ex)
                    {
                        Console.WriteLine(ex.Message, ex.Source);
                        Console.WriteLine("Output will be sent to console instead");
                    }
                }

                using (scriptWriter)
                {
                    // Call appropriate Partition Manager constructor depending on whether a partition number or range value is provided
                    if (partitionNumber != 0)
                    {
                        pm = new PartitionManagement.PartitionManager(sc, dbName, schName, partitionTblName,
                                                                      stagingTblName, partitionNumber, scriptWriter, executeCommands);
                    }
                    else
                    {
                        pm = new PartitionManagement.PartitionManager(sc, dbName, schName, partitionTblName,
                                                                      stagingTblName, partitionRangeValue, scriptWriter, executeCommands);
                    }

                    pm.SetTransactionIsolationLevelReaduncommitted();

                    switch (command)
                    {
                    case "clearpartition":
                        pm.CreateStgTable();
                        pm.CreateStgFkeys();
                        pm.CreateStgChecks();
                        pm.CreateStgPartitionCheck();
                        // If staging table is being deleted, no need to create non-clustered indexes & views
                        pm.CreateStgIndexes(keepStaging);
                        pm.ClearPartition(keepStaging);
                        if (generateScript)
                        {
                            pm.outputScript();
                        }
                        break;

                    case "createstagingfull":
                        pm.CreateStgTable();
                        pm.CreateStgFkeys();
                        pm.CreateStgChecks();
                        pm.CreateStgPartitionCheck();
                        pm.CreateStgIndexes(true);
                        if (generateScript)
                        {
                            pm.outputScript();
                        }
                        break;

                    case "createstagingclusteredindex":
                        pm.CreateStgTable();
                        pm.CreateStgFkeys();
                        pm.CreateStgChecks();
                        pm.CreateStgPartitionCheck();
                        pm.CreateStgIndexes(false);
                        if (generateScript)
                        {
                            pm.outputScript();
                        }
                        break;

                    case "createstagingnoindex":
                        pm.CreateStgTable();
                        pm.CreateStgFkeys();
                        pm.CreateStgChecks();
                        pm.CreateStgPartitionCheck();
                        if (generateScript)
                        {
                            pm.outputScript();
                        }
                        break;

                    case "indexstaging":
                        pm.CreateStgIndexes(true);
                        if (generateScript)
                        {
                            pm.outputScript();
                        }
                        break;

                    default:
                        throw new System.InvalidOperationException("Invalid command choice\nCommand Choices: ClearPartition | CreateStagingFull | CreateStagingClusteredIndex | CreateStagingNoindex | IndexStaging");
                    }
                }
            }
            catch (Exception e)
            {
                sc.Disconnect();
                sc = null;
                Console.WriteLine(e);
                throw e;
            }
            sc.Disconnect();
            sc = null;
        }
        void Run(ref AppArgs parsedArgs)
        {
            ServerConnection sc = new ServerConnection();

              try
              {
             sc.ServerInstance = parsedArgs.Server;
             sc.LoginSecure = parsedArgs.Integrated;
             sc.StatementTimeout = 0;

             if (!parsedArgs.Integrated)
             {
                sc.Login = parsedArgs.User;
                sc.Password = parsedArgs.Password;
             }
             sc.Connect();

             String command = parsedArgs.Command.ToLower();
             String dbName = parsedArgs.Database;
             String schName = parsedArgs.Schema;
             String partitionTblName = parsedArgs.PartitionTable;
             String scriptOption = (parsedArgs.ScriptOption == null) ? null : parsedArgs.ScriptOption.ToLower();

             // Set flag indicating whether scripts will be generated
             bool generateScript = (scriptOption == "i" || scriptOption == "o") ? true : false;
             // set flag indicated whether commands will be executed on SQL Server (as opposed to just scripting)
             bool executeCommands = (scriptOption == "o") ? false : true;

             // Ensure that either a partition number or range value is provided, not both
             int partitionNumber = parsedArgs.PartitionNumber;
             string partitionRangeValue = parsedArgs.PartitionRangeValue;
             if (((partitionRangeValue == null) && (partitionNumber == 0)) ||
                ((partitionRangeValue != null) && (partitionNumber != 0)))
             {
                throw new System.ArgumentException("Specify either a partition number OR a partition range value");
             }

             // Construct a staging table name likely to be unique, if no name is provided
             String stagingTblName = parsedArgs.StagingTable;
             if (stagingTblName == null)
             {
                stagingTblName = partitionTblName + "_part" + partitionNumber.ToString("####", System.Globalization.CultureInfo.InvariantCulture)
                    + partitionRangeValue + "_" + System.DateTime.Now.Ticks.ToString(System.Globalization.CultureInfo.InvariantCulture);
             }

             bool keepStaging = parsedArgs.Keep;
             PartitionManager pm = null;

             System.IO.StreamWriter scriptWriter = null;

             if ((scriptOption == "i" || scriptOption == "o")
                && parsedArgs.ScriptFile != null)
             {
                try
                {
                    scriptWriter = new System.IO.StreamWriter(parsedArgs.ScriptFile, true, Encoding.Unicode);
                }
                catch (System.IO.IOException ex)
                {
                    Console.WriteLine(ex.Message, ex.Source);
                    Console.WriteLine("Output will be sent to console instead");
                }
             }

             using (scriptWriter)
             {
                // Call appropriate Partition Manager constructor depending on whether a partition number or range value is provided
                if (partitionNumber != 0)
                {
                    pm = new PartitionManagement.PartitionManager(sc, dbName, schName, partitionTblName,
                       stagingTblName, partitionNumber, scriptWriter, executeCommands);
                }
                else
                {
                    pm = new PartitionManagement.PartitionManager(sc, dbName, schName, partitionTblName,
                       stagingTblName, partitionRangeValue, scriptWriter, executeCommands);
                }

                pm.SetTransactionIsolationLevelReaduncommitted();

                switch (command)
                {
                    case "clearpartition":
                       pm.CreateStgTable();
                       pm.CreateStgFkeys();
                       pm.CreateStgChecks();
                       pm.CreateStgPartitionCheck();
                       // If staging table is being deleted, no need to create non-clustered indexes & views
                       pm.CreateStgIndexes(keepStaging);
                       pm.ClearPartition(keepStaging);
                       if (generateScript) pm.outputScript();
                       break;
                    case "createstagingfull":
                       pm.CreateStgTable();
                       pm.CreateStgFkeys();
                       pm.CreateStgChecks();
                       pm.CreateStgPartitionCheck();
                       pm.CreateStgIndexes(true);
                       if (generateScript) pm.outputScript();
                       break;
                    case "createstagingclusteredindex":
                       pm.CreateStgTable();
                       pm.CreateStgFkeys();
                       pm.CreateStgChecks();
                       pm.CreateStgPartitionCheck();
                       pm.CreateStgIndexes(false);
                       if (generateScript) pm.outputScript();
                       break;
                    case "createstagingnoindex":
                       pm.CreateStgTable();
                       pm.CreateStgFkeys();
                       pm.CreateStgChecks();
                       pm.CreateStgPartitionCheck();
                       if (generateScript) pm.outputScript();
                       break;
                    case "indexstaging":
                       pm.CreateStgIndexes(true);
                       if (generateScript) pm.outputScript();
                       break;
                    default:
                       throw new System.InvalidOperationException("Invalid command choice\nCommand Choices: ClearPartition | CreateStagingFull | CreateStagingClusteredIndex | CreateStagingNoindex | IndexStaging");
                }
             }
              }
              catch (Exception e)
              {
             sc.Disconnect();
             sc = null;
             Console.WriteLine(e);
             throw e;
              }
              sc.Disconnect();
              sc = null;
        }