Exemple #1
0
        void SqlServerConfiguration_Load(object sender, EventArgs e)
        {
            refreshServersButton.Visible = Program.ShouldEnumerateSqlServers;
            labelDataSource.Visible      = !Program.ShouldEnumerateSqlServers;
            this.Cursor = Cursors.WaitCursor;

            string selectedServer = serverNameCoboBox.Text;

            try
            {
                if (Program.ShouldEnumerateSqlServers)
                {
                    if (!_DesignMode)
                    {
                        AutoResetEvent enumServersEvent = new AutoResetEvent(false);
                        Exception      error            = null;
                        Thread         getServersThread;

                        getServersThread = new Thread(delegate()
                        {
                            try
                            {
                                servers = SqlUtilities.GetServerList();
                            }
                            catch (Exception exp)
                            {
                                error = exp;
                            }
                            finally
                            {
                                enumServersEvent.Set();
                            }
                        });

                        if (servers == null)
                        {
                            getServersThread.Start();


                            EnumenratingSQLServersDialog enumDlg = new EnumenratingSQLServersDialog();

                            enumDlg.Show(this);

                            enumServersEvent.WaitOne();

                            enumDlg.Close();
                        }

                        if (null != error)
                        {
                            throw error;
                        }
                        else
                        {
                            string[] localServers = SqlUtilities.GetLocalSQLServerInstances();

                            if (servers.Count > 0)
                            {
                                serverNameCoboBox.DataSource = servers;

                                if (localServers != null && localServers.Length > 0)
                                {
                                    serverNameCoboBox.Text = localServers[0];
                                }
                            }
                            else
                            {
                                serverNameCoboBox.DataSource = localServers;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);

                serverNameCoboBox.DataSource = SqlUtilities.GetLocalSQLServerInstances( );

                Messager.ShowWarning(this,
                                     "The application can't enumerate the SQL Servers located in your network because \"Microsoft SQL Server 2008 Management Objects (SMO)\" is not installed.\n" +
                                     "You can manually type the SQL Server name in the server name field or install Microsoft SMO from Microsoft site:\n" +
                                     "http://www.microsoft.com/downloadS/details.aspx?familyid=C6C3E9EF-BA29-4A43-8D69-A2BED18FE73C&displaylang=en");

                return;
            }
            finally
            {
                if (!string.IsNullOrEmpty(selectedServer))
                {
                    serverNameCoboBox.Text = selectedServer;
                }

                this.Cursor = Cursors.Default;
            }
        }