Пример #1
0
 private static void Main(string[] args)
 {
     ConsoleHandler.Initialize();
     ConsoleUtil.ResizeConsoleInputBuffer(0x6400);
     try
     {
         try
         {
             CommandLineParser.ParseCommandArgs(args);
         }
         catch (CommandParserException exception)
         {
             if (!string.IsNullOrEmpty(exception.Message))
             {
                 Console.WriteLine(exception.Message);
             }
             return;
         }
         catch (Exception ex)
         {
             if (!string.IsNullOrEmpty(ex.Message))
             {
                 Console.WriteLine(ex.Message);
             }
             return;
         }
         if (CommandLineParser.Command == null)
         {
             ShowHelpCommand.ExecuteDirect();
         }
         else
         {
             var command = CommandLineParser.Command as ShowHelpCommand;
             if (command != null)
             {
                 ((ISimpleCommand)CommandLineParser.Command).Execute();
             }
             else
             {
                 if (!string.IsNullOrEmpty(CommandLineParser.LogFilePath))
                 {
                     CommandLineParser.CleanupArguments(args);
                     ConsoleHandler.StartLog(CommandLineParser.LogFilePath, ProgramInfo.ExecutableName, new string[0]);
                 }
                 try
                 {
                     var engineCommand = CommandLineParser.Command as IEngineCommand;
                     if (engineCommand != null)
                     {
                         new Engine(CommandLineParser.ConnectionOptions).Execute(engineCommand);
                     }
                     else
                     {
                         var simpleCommand = CommandLineParser.Command as ISimpleCommand;
                         if (simpleCommand != null)
                         {
                             simpleCommand.Execute();
                         }
                     }
                 }
                 catch (Exception exception2)
                 {
                     Console.WriteLine(exception2.Message);
                 }
             }
         }
     }
     finally
     {
         ConsoleHandler.Cleanup();
     }
 }
        internal static bool ProcessException(SqlConnection connection, SqlException ex)
        {
            SqlConnectionStringBuilder builder     = new SqlConnectionStringBuilder(connection.ConnectionString);
            SqlDataSourceInformation   information = SqlDataSourceInformation.Parse(builder.DataSource);

            if ((!string.IsNullOrEmpty(information.ServerName) && information.VerifyIsLocal()) && !string.IsNullOrEmpty(information.InstanceName))
            {
                string             serviceName = "MSSQL$" + information.InstanceName;
                ServiceStatusError error       = CheckServiceError(serviceName);
                string             str2        = null;
                switch (error)
                {
                case ServiceStatusError.Notinstalled:
                    str2 = "The service '" + serviceName + "' is not installed.";
                    break;

                case ServiceStatusError.Notstarted:
                {
                    str2 = "The service '" + serviceName + "' is not started.";
                    string str3 = "";
                    Console.WriteLine(str2);
                    if (!ScriptEngine.IsExecutingScript)
                    {
                        while (!str3.Equals("y", StringComparison.OrdinalIgnoreCase) && !str3.Equals("n", StringComparison.OrdinalIgnoreCase))
                        {
                            str3 = ConsoleUtil.PromptUser("Would you like to start the service (y/n)?");
                            Console.WriteLine("");
                            if (str3.Equals("y", StringComparison.OrdinalIgnoreCase))
                            {
                                ServiceController controller = new ServiceController(serviceName);
                                Console.Write("Starting service...");
                                try
                                {
                                    controller.Start();
                                    controller.WaitForStatus(ServiceControllerStatus.Running);
                                    Thread.Sleep(0x1388);
                                }
                                catch
                                {
                                    Console.WriteLine("");
                                }
                                if (controller.Status == ServiceControllerStatus.Running)
                                {
                                    Console.WriteLine(" Done.");
                                    return(true);
                                }
                                Console.WriteLine(" Failed.");
                                return(false);
                            }
                        }
                    }
                    break;
                }
                }
                if (str2 != null)
                {
                    throw new Exception(ex.Message + Environment.NewLine + Environment.NewLine + "Additional information:" + Environment.NewLine + str2, ex);
                }
            }
            throw ex;
        }
Пример #3
0
 internal bool Initialize(ConnectionOptions connectionOptions)
 {
     this._connectionOptions = connectionOptions;
     if (connectionOptions.ServerName == null)
     {
         if (ProgramInfo.InSqlMode)
         {
             string[] localInstances = null;
             try
             {
                 localInstances = SqlServerEnumerator.GetLocalInstances();
             }
             catch (Exception)
             {
             }
             if ((localInstances != null) && (localInstances.Length > 0))
             {
                 this._connectionOptions.ServerName = localInstances[0];
             }
             else
             {
                 this._connectionOptions.ServerName = ".";
             }
         }
         else
         {
             this._connectionOptions.ServerName = @".\SQLEXPRESS";
         }
     }
     if (connectionOptions.PromptForPassword)
     {
         try
         {
             this._connectionOptions.Password = ConsoleUtil.PromptForPassword("Enter password for user '" + connectionOptions.UserName + "': ");
             Console.WriteLine("");
         }
         catch (UserCanceledException)
         {
             return(false);
         }
     }
     if (!string.IsNullOrEmpty(connectionOptions.RunningAs) && !connectionOptions.UseMainInstance)
     {
         IDbConnection connection = this.BuildMasterConnection(true);
         string        str        = "SELECT owning_principal_name, instance_pipe_name FROM sys.dm_os_child_instances";
         IDbCommand    command    = DataUtil.CreateCommand(this, connection);
         command.CommandText = str;
         connection.Open();
         try
         {
             SqlDataReader reader = (SqlDataReader)command.ExecuteReader();
             if (reader != null)
             {
                 try
                 {
                     if (reader.HasRows)
                     {
                         int ordinal = reader.GetOrdinal("owning_principal_name");
                         int num2    = reader.GetOrdinal("instance_pipe_name");
                         while (reader.Read())
                         {
                             if (string.Equals(reader.GetString(ordinal), connectionOptions.RunningAs, StringComparison.OrdinalIgnoreCase))
                             {
                                 string str2 = reader.GetString(num2);
                                 if (this._originalServerName == null)
                                 {
                                     Console.WriteLine("Using instance '" + str2 + "'.");
                                     Console.WriteLine("");
                                     this._originalServerName                = connectionOptions.ServerName;
                                     this._connectionOptions.ServerName      = str2;
                                     this._connectionOptions.UseMainInstance = true;
                                 }
                                 else
                                 {
                                     Console.WriteLine("Multiple child instances were found to run under the principal name you specified. The first one will be used.");
                                 }
                             }
                         }
                     }
                     if (this._originalServerName == null)
                     {
                         throw new Exception("No child instance is running under the principal name you specified.");
                     }
                 }
                 finally
                 {
                     reader.Close();
                 }
             }
         }
         finally
         {
             connection.Close();
         }
     }
     return(true);
 }