Example #1
0
        private int AddSqlService(int serverId)
        {
            int serviceId = -1;

            try
            {
                Log.WriteStart("Adding Sql service");

                SqlServerItem item       = ParseConnectionString(EnterpriseServerSetup.DbInstallConnectionString);
                string        serverName = item.Server.ToLower();
                if (serverName.StartsWith("(local)") ||
                    serverName.StartsWith("localhost") ||
                    serverName.StartsWith(System.Environment.MachineName.ToLower()))
                {
                    ServiceInfo serviceInfo = new ServiceInfo();
                    serviceInfo.ServerId    = serverId;
                    serviceInfo.ServiceName = "SQL Server";
                    serviceInfo.Comments    = string.Empty;

                    string connectionString = EnterpriseServerSetup.DbInstallConnectionString;
                    //check SQL version
                    if (SqlUtils.CheckSqlConnection(connectionString))
                    {
                        // check SQL server version
                        string sqlVersion = SqlUtils.GetSqlServerVersion(connectionString);
                        if (sqlVersion.StartsWith("9."))
                        {
                            serviceInfo.ProviderId = 16;
                        }
                        else if (sqlVersion.StartsWith("10."))
                        {
                            serviceInfo.ProviderId = 202;
                        }
                        else if (sqlVersion.StartsWith("11."))
                        {
                            serviceInfo.ProviderId = 209;
                        }
                        else if (sqlVersion.StartsWith("12."))
                        {
                            serviceInfo.ProviderId = 1203;
                        }
                        serviceId = ES.Services.Servers.AddService(serviceInfo);
                    }
                    else
                    {
                        Log.WriteInfo("SQL Server connection error");
                    }
                    //configure service
                    if (serviceId > 0)
                    {
                        StringDictionary settings = GetServiceSettings(serviceId);
                        if (settings != null)
                        {
                            settings["InternalAddress"]      = item.Server;
                            settings["ExternalAddress"]      = string.Empty;
                            settings["UseTrustedConnection"] = item.WindowsAuthentication.ToString();
                            settings["SaLogin"]    = item.User;
                            settings["SaPassword"] = item.Password;
                            UpdateServiceSettings(serviceId, settings);
                        }
                        InstallService(serviceId);
                        Log.WriteEnd("Added Sql service");
                    }
                    else
                    {
                        Log.WriteError(string.Format("Enterprise Server error: {0}", serviceId));
                    }
                }
                else
                {
                    Log.WriteError("Microsoft SQL Server was not found");
                }
                return(serviceId);
            }
            catch (Exception ex)
            {
                if (!Utils.IsThreadAbortException(ex))
                {
                    Log.WriteError("Sql service configuration error", ex);
                }
                return(-1);
            }
        }