/// <summary>
 /// Returns MSSQL as a dependency if it is installed, otherwise returns null.  This allows the service to work
 /// better if SQL is installed, but also allows the service to be installed (not fail) if SQL isn't installed.
 /// (Not having SQL installed is common for proper hosted services, where SQL is in the tier-2 servers.)
 /// </summary>
 private string[] GetDependencies()
 {
     if (DBSetupHelper.SelectDatabaseInstance(out this.sqlInstanceName, out this.sqlServiceName))
     {
         sqlFound = true;
         return(new string[] { sqlServiceName });
     }
     else
     {
         sqlFound = false;
         return(null);
     }
 }
        public static bool SelectDatabaseInstance(out string sqlInstanceName, out string sqlServiceName)
        {
            //Look up SQL Server instances from the registry
            string[] sqlInstances = DBSetupHelper.GetDBInstances();
            sqlServiceName  = null;
            sqlInstanceName = null;

            if (sqlInstances.Length == 0)
            {
                Trace.WriteLine("No DB instance found.");
                return(false);
            }

            // Do sanity check on the instances.  Accept the first one for which a service exists.
            foreach (string name in sqlInstances)
            {
                ServiceController       sqlController;
                ServiceControllerStatus sqlStatus;
                try {
                    Console.WriteLine("Checking instance name: " + name);
                    sqlController   = new ServiceController(name);
                    sqlStatus       = sqlController.Status; // should throw if the service does't exist
                    sqlInstanceName = name;
                    sqlServiceName  = name;
                }
                catch {
                    try {
                        Console.WriteLine("Checking instance name: " + "$MSSQL$" + name);
                        sqlController   = new ServiceController("MSSQL$" + name);
                        sqlStatus       = sqlController.Status; // should throw if the service does't exist
                        sqlInstanceName = name;
                        sqlServiceName  = "MSSQL$" + name;
                    }
                    catch {
                        continue;
                    }
                }

                Trace.WriteLine("Selected service: " + sqlServiceName + "; instance=" + sqlInstanceName);
                return(true);
            }
            Trace.WriteLine("No valid DB instance found.");
            return(false);
        }
        public override void Install(IDictionary savedState)
        {
            CheckAdministratorRole();

            #region Add the firewall punchthrough
            try
            {
                MSR.LST.Net.FirewallUtility.AddFirewallException(portMapName,
                                                                 ports, portTypes, Context.Parameters["assemblypath"]);
            }
            catch (NotSupportedException ex)
            {
                RtlAwareMessageBox.Show(null, string.Format(CultureInfo.CurrentCulture,
                                                            Strings.FirewallExceptionAddingText, ex.Message), Strings.FirewallExceptionFailedTitle,
                                        MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                                        (MessageBoxOptions)0);
            }
            #endregion

            #region Check for no SQL and show the error message here

            if (!this.sqlFound) // Show the error msg here that we couldn't show earlier
            {
                RtlAwareMessageBox.Show(null, Strings.SqlServerNotFoundText, Strings.SqlServerNotFoundTitle,
                                        MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0);
            }
            else // only try to create the database if we know SQL is installed
            {
                CreateDatabase();
                try {
                    DBSetupHelper.SetConfigFiles(InstallPath, this.sqlInstanceName);
                }
                catch (Exception ex) {
                    RtlAwareMessageBox.Show(null, ex.ToString(), Strings.ConfigurationFileCustomizationFailed,
                                            MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                                            (MessageBoxOptions)0);
                }
            }

            #endregion

            base.Install(savedState);
        }
        private void RemoveDatabase()
        {
            // Verify the user wants to delete the database
            DialogResult dr = RtlAwareMessageBox.Show(null, Strings.DeleteDatabaseText, Strings.DeleteDatabaseTitle,
                                                      MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0);

            if (dr != DialogResult.Yes)
            {
                return;
            }

            try
            {
                DBSetupHelper.RemoveDatabase(InstallPath);
            }
            catch (Exception ex)
            {
                RtlAwareMessageBox.Show(null, string.Format(CultureInfo.CurrentCulture, Strings.RunDropDatabaseSQL,
                                                            ex.ToString()), string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error,
                                        MessageBoxDefaultButton.Button1, (MessageBoxOptions)0);
            }
        }
        /// <summary>
        /// Creates the database and the schema, but does not create the stored procedures.  That's done in CreateStoredProcedures().  Because
        /// wiping the schema & database would cause the user to lose all of their data, this is an optional activity.
        /// </summary>
        private void CreateDatabase()
        {
            // The installer context should tell us whether to create the database.
            // This is the value from the radio buttons in the setup dialog.
            string crdb = this.Context.Parameters["createdatabase"];

            if ((crdb != null) && crdb.Equals("YES"))
            {
                Console.WriteLine("\n" + Strings.PleaseWaitDatabase);

                try {
                    DBSetupHelper.InstallDatabase(InstallPath, this.sqlInstanceName, this.sqlServiceName);
                }
                catch (Exception ex) {
                    RtlAwareMessageBox.Show(null, string.Format(CultureInfo.CurrentCulture,
                                                                Strings.DatabaseOperationFailedText, ex.ToString()), Strings.DatabaseOperationFailedTitle,
                                            MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                                            (MessageBoxOptions)0);
                }

                Console.WriteLine(Strings.DatabaseInitializationComplete + "\n");
            }
        }