Exemple #1
0
 private void ExecuteSqlCommands(List <string> commandsToExecute, ProcessingOption option)
 {
     if (commandsToExecute.Count > 0)
     {
         string str = "";
         for (int i = 0; i < commandsToExecute.Count; i++)
         {
             str = str + commandsToExecute[i];
             if (i < (commandsToExecute.Count - 1))
             {
                 str = str + ";";
             }
         }
         commandsToExecute.Clear();
         try
         {
             SqlCommand command = (SqlCommand)DataUtil.CreateCommand(this._engine.ConnectionManager, this._connection);
             if (this._engine.ConnectionManager.ConnectionOptions.CommandTimeout >= 0)
             {
                 command.CommandTimeout = this._engine.ConnectionManager.ConnectionOptions.CommandTimeout;
             }
             command.CommandText = str;
             Stopwatch     stopwatch = Stopwatch.StartNew();
             SqlDataReader reader    = command.ExecuteReader();
             if (reader != null)
             {
                 try
                 {
                     long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
                     stopwatch.Stop();
                     int num3 = 1;
                     do
                     {
                         if (num3 > 1)
                         {
                             Console.WriteLine("");
                             Console.WriteLine("Results #" + num3);
                             Console.WriteLine("==========================");
                         }
                         if (reader.FieldCount > 0)
                         {
                             Console.WriteLine("");
                             Console.WriteLine(DataUtil.FetchAndDisplayRows(reader, option, out this.LastTable, out this.LastScalar) + " row(s) affected.");
                         }
                         else if (reader.RecordsAffected >= 0)
                         {
                             Console.WriteLine("");
                             Console.WriteLine(reader.RecordsAffected + " row(s) affected.");
                         }
                         else if (!Global.SilentMode)
                         {
                             Console.WriteLine("Command completed successfully.");
                         }
                         if (this._timerEnabled)
                         {
                             Console.WriteLine("");
                             Console.WriteLine(elapsedMilliseconds + " milliseconds.");
                         }
                         num3++;
                     }while (reader.NextResult());
                 }
                 finally
                 {
                     try
                     {
                         reader.Close();
                     }
                     catch
                     {
                     }
                 }
             }
         }
         catch (Exception exception)
         {
             Console.WriteLine("Error executing commands:" + Environment.NewLine);
             DataUtil.DisplayException(exception);
             commandsToExecute.Clear();
         }
     }
 }
Exemple #2
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);
 }