private static IEnumerable <string> GetPhysicalFilesForServer(SqlLocalDbInstance existingInstance)
        {
            if (!existingInstance.IsRunning)
            {
                existingInstance.Start();
            }

            List <string> filePaths = new List <string>();

            using (var connection = existingInstance.CreateConnection())
                using (var command = connection.CreateCommand())
                {
                    connection.Open();
                    command.CommandText = "SELECT physical_name AS physicalPath FROM sys.master_files";
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            filePaths.Add(reader["physicalPath"].ToString());
                        }
                    }
                }

            return(filePaths);
        }
        /// <summary>
        /// Stops the instance.
        /// </summary>
        public void StopInstance()
        {
            if (iisExpressProcess != null)
            {
                iisExpressProcess.Kill();
                iisExpressProcess = null;
            }

            if (RunningInstanceName != null)
            {
                try
                {
                    using (var connection = localDb.CreateConnection())
                    {
                        connection.Open();

                        //
                        // Shrink the log file.
                        //
                        connection.ChangeDatabase(RunningInstanceName);
                        var cmd = connection.CreateCommand();
                        cmd.CommandText = "DBCC SHRINKFILE ([Database_log.ldf], 1)";
                        cmd.ExecuteNonQuery();

                        //
                        // Detach the database.
                        //
                        connection.ChangeDatabase("master");
                        cmd             = connection.CreateCommand();
                        cmd.CommandText = string.Format("exec sp_detach_db [{0}]", RunningInstanceName);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch
                {
                    /* Intentionally left blank */
                }

                RunningInstanceName = null;
            }

            Dispatcher.Invoke(() =>
            {
                txtStatus.Text = "Idle";
                UpdateState();
            });
        }
Exemple #3
0
        /// <summary>
        /// Start up a new IIS Express instance for the Rock instance that is selected.
        /// </summary>
        /// <param name="sender">The object that is sending this event.</param>
        /// <param name="e">The arguments that describe the event.</param>
        private void btnStartStop_Click(object sender, RoutedEventArgs e)
        {
            if (btnStartStop.Content.ToString().EndsWith("Start"))
            {
                txtStatus.Text  = "Starting...";
                txtConsole.Text = string.Empty;

                //
                // Just in case something goes wrong and they managed to type in a non-numeric.
                //
                if (!int.TryParse(txtPort.Text, out int port))
                {
                    port = 6229;
                }

                //
                // Find the path to the RockWeb instance.
                //
                var items = (List <string>)cbInstances.ItemsSource;
                var path  = Path.Combine(Support.GetInstancesPath(), items[cbInstances.SelectedIndex], "RockWeb");

                //
                // Check if the Database file already exists and if not create the
                // Run.Migration file so Rock initializes itself.
                //
                var dbPath = Path.Combine(path, "App_Data", "Database.mdf");
                if (!File.Exists(dbPath))
                {
                    var runMigrationPath = Path.Combine(path, "App_Data", "Run.Migration");
                    File.WriteAllText(runMigrationPath, string.Empty);
                }

                ConfigureConnectionString(path);

                //
                // Launch the IIS Express process for this RockWeb.
                //
                iisExpressProcess = new ConsoleApp(GetIisExecutable());
                iisExpressProcess.ProcessExited        += IisExpressProcess_Exited;
                iisExpressProcess.StandardTextReceived += IisExpressProcess_StandardTextReceived;
                iisExpressProcess.ExecuteAsync(String.Format("/path:{0}", path), String.Format("/port:{0}", port));

                //
                // Update the status text to contain a clickable link to the instance.
                //
                var linkText = string.Format("http://localhost:{0}/", port);
                var link     = new Hyperlink(new Run(linkText))
                {
                    NavigateUri = new Uri(linkText)
                };
                link.RequestNavigate += StatusLink_RequestNavigate;
                txtStatus.Inlines.Clear();
                txtStatus.Inlines.Add(new Run("Running at "));
                txtStatus.Inlines.Add(link);
            }
            else
            {
                if (iisExpressProcess != null)
                {
                    iisExpressProcess.Kill();
                    iisExpressProcess = null;
                }

                try
                {
                    var items = (List <string>)cbInstances.ItemsSource;

                    using (var connection = localDb.CreateConnection())
                    {
                        connection.Open();

                        //
                        // Shrink the log file.
                        //
                        connection.ChangeDatabase(items[cbInstances.SelectedIndex]);
                        var cmd = connection.CreateCommand();
                        cmd.CommandText = "DBCC SHRINKFILE ([Database_log.ldf], 1)";
                        cmd.ExecuteNonQuery();

                        //
                        // Detach the database.
                        //
                        connection.ChangeDatabase("master");
                        cmd             = connection.CreateCommand();
                        cmd.CommandText = string.Format("exec sp_detach_db [{0}]", items[cbInstances.SelectedIndex]);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch
                {
                    /* Intentionally left blank */
                }

                txtStatus.Text = "Idle";
            }

            UpdateState();
        }