NET 获取局域网内所有SQL Server 服务器列表

using System;
using System.ComponentModel;
using System.Windows.Forms;
using SQLDMO;
using Application = System.Windows.Forms.Application;

namespace SQLDMOSample
{
    
/// <summary>
    
/// Summary description for Form1.
    
/// </summary>
    
/// 



    
public class Form1 : Form
    
{
        
private SQLDMO.Application sqlApp = new ApplicationClass();
        NameList sqlServers
=null;
        
private ComboBox cboServers;
        
private Label label1;
        
private Label label2;
        
private TextBox txtUser;
        
private Label label3;
        
private TextBox txtPassword;
        
private ListBox lstObjects;
        
private ComboBox cboDatabase;
        
private Label label4;
        
private LinkLabel linkLabel2;
        
private GroupBox groupBox1;
        
private LinkLabel linkLabel1;
        
private LinkLabel linkLabel3;
        
private LinkLabel linkLabel4;
        
/// <summary>
        
/// Required designer variable.
        
/// </summary>

        private Container components = null;

        
public Form1()
        
{
            
//
            
// Required for Windows Form Designer support
            
//
            InitializeComponent();

            
//
            
// TODO: Add any constructor code after InitializeComponent call
            
//
        }


        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
#region Windows Form Designer generated code
        
/// <summary>
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// </summary>

        private void InitializeComponent()
        
{
            
this.cboServers = new System.Windows.Forms.ComboBox();
            
this.label1 = new System.Windows.Forms.Label();
            
this.label2 = new System.Windows.Forms.Label();
            
this.txtUser = new System.Windows.Forms.TextBox();
            
this.label3 = new System.Windows.Forms.Label();
            
this.txtPassword = new System.Windows.Forms.TextBox();
            
this.lstObjects = new System.Windows.Forms.ListBox();
            
this.cboDatabase = new System.Windows.Forms.ComboBox();
            
this.label4 = new System.Windows.Forms.Label();
            
this.linkLabel2 = new System.Windows.Forms.LinkLabel();
            
this.groupBox1 = new System.Windows.Forms.GroupBox();
            
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
            
this.linkLabel3 = new System.Windows.Forms.LinkLabel();
            
this.linkLabel4 = new System.Windows.Forms.LinkLabel();
            
this.groupBox1.SuspendLayout();
            
this.SuspendLayout();
            
// 
            
// cboServers
            
// 
            this.cboServers.Location = new System.Drawing.Point(824);
            
this.cboServers.Name = "cboServers";
            
this.cboServers.Size = new System.Drawing.Size(24021);
            
this.cboServers.TabIndex = 0;
            
this.cboServers.SelectedIndexChanged += new System.EventHandler(this.cboServers_SelectedIndexChanged);
            
// 
            
// label1
            
// 
            this.label1.Location = new System.Drawing.Point(88);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(24016);
            
this.label1.TabIndex = 1;
            
this.label1.Text = "SQL Servers";
            
// 
            
// label2
            
// 
            this.label2.Location = new System.Drawing.Point(848);
            
this.label2.Name = "label2";
            
this.label2.Size = new System.Drawing.Size(12016);
            
this.label2.TabIndex = 2;
            
this.label2.Text = "User Name";
            
// 
            
// txtUser
            
// 
            this.txtUser.Location = new System.Drawing.Point(864);
            
this.txtUser.Name = "txtUser";
            
this.txtUser.Size = new System.Drawing.Size(24020);
            
this.txtUser.TabIndex = 3;
            
this.txtUser.Text = "sa";
            
// 
            
// label3
            
// 
            this.label3.Location = new System.Drawing.Point(888);
            
this.label3.Name = "label3";
            
this.label3.Size = new System.Drawing.Size(8816);
            
this.label3.TabIndex = 4;
            
this.label3.Text = "Password";
            
// 
            
// txtPassword
            
// 
            this.txtPassword.Location = new System.Drawing.Point(8104);
            
this.txtPassword.Name = "txtPassword";
            
this.txtPassword.Size = new System.Drawing.Size(24020);
            
this.txtPassword.TabIndex = 5;
            
this.txtPassword.Text = "";
            
// 
            
// lstObjects
            
// 
            this.lstObjects.HorizontalScrollbar = true;
            
this.lstObjects.Location = new System.Drawing.Point(2568);
            
this.lstObjects.Name = "lstObjects";
            
this.lstObjects.Size = new System.Drawing.Size(240290);
            
this.lstObjects.TabIndex = 6;
            
// 
            
// cboDatabase
            
// 
            this.cboDatabase.Enabled = false;
            
this.cboDatabase.Location = new System.Drawing.Point(8152);
            
this.cboDatabase.Name = "cboDatabase";
            
this.cboDatabase.Size = new System.Drawing.Size(24021);
            
this.cboDatabase.TabIndex = 8;
            
this.cboDatabase.SelectedIndexChanged += new System.EventHandler(this.cboDatabase_SelectedIndexChanged);
            
// 
            
// label4
            
// 
            this.label4.Location = new System.Drawing.Point(8136);
            
this.label4.Name = "label4";
            
this.label4.Size = new System.Drawing.Size(8816);
            
this.label4.TabIndex = 9;
            
this.label4.Text = "Databases";
            
// 
            
// linkLabel2
            
// 
            this.linkLabel2.Location = new System.Drawing.Point(168136);
            
this.linkLabel2.Name = "linkLabel2";
            
this.linkLabel2.Size = new System.Drawing.Size(8016);
            
this.linkLabel2.TabIndex = 10;
            
this.linkLabel2.TabStop = true;
            
this.linkLabel2.Text = "Connect";
            
this.linkLabel2.TextAlign = System.Drawing.ContentAlignment.TopRight;
            
this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
            
// 
            
// groupBox1
            
// 
            this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    
this.linkLabel4,
                                                                                    
this.linkLabel3,
                                                                                    
this.linkLabel1}
);
            
this.groupBox1.Enabled = false;
            
this.groupBox1.Location = new System.Drawing.Point(8176);
            
this.groupBox1.Name = "groupBox1";
            
this.groupBox1.Size = new System.Drawing.Size(240120);
            
this.groupBox1.TabIndex = 11;
            
this.groupBox1.TabStop = false;
            
// 
            
// linkLabel1
            
// 
            this.linkLabel1.Location = new System.Drawing.Point(15216);
            
this.linkLabel1.Name = "linkLabel1";
            
this.linkLabel1.Size = new System.Drawing.Size(7216);
            
this.linkLabel1.TabIndex = 0;
            
this.linkLabel1.TabStop = true;
            
this.linkLabel1.Text = "Get Tables";
            
this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.TopRight;
            
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
            
// 
            
// linkLabel3
            
// 
            this.linkLabel3.Location = new System.Drawing.Point(10440);
            
this.linkLabel3.Name = "linkLabel3";
            
this.linkLabel3.Size = new System.Drawing.Size(12016);
            
this.linkLabel3.TabIndex = 1;
            
this.linkLabel3.TabStop = true;
            
this.linkLabel3.Text = "Get Stored Procedures";
            
this.linkLabel3.TextAlign = System.Drawing.ContentAlignment.TopRight;
            
this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);
            
// 
            
// linkLabel4
            
// 
            this.linkLabel4.Location = new System.Drawing.Point(11264);
            
this.linkLabel4.Name = "linkLabel4";
            
this.linkLabel4.Size = new System.Drawing.Size(11216);
            
this.linkLabel4.TabIndex = 2;
            
this.linkLabel4.TabStop = true;
            
this.linkLabel4.Text = "Views";
            
this.linkLabel4.TextAlign = System.Drawing.ContentAlignment.TopRight;
            
this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            
this.ClientSize = new System.Drawing.Size(504301);
            
this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          
this.groupBox1,
                                                                          
this.linkLabel2,
                                                                          
this.label4,
                                                                          
this.cboDatabase,
                                                                          
this.lstObjects,
                                                                          
this.txtPassword,
                                                                          
this.label3,
                                                                          
this.txtUser,
                                                                          
this.label2,
                                                                          
this.label1,
                                                                          
this.cboServers}
);
            
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            
this.MaximizeBox = false;
            
this.Name = "Form1";
            
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            
this.Text = "SQLDMO Example";
            
this.Load += new System.EventHandler(this.Form1_Load);
            
this.groupBox1.ResumeLayout(false);
            
this.ResumeLayout(false);

        }

        
#endregion


        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void cboServers_SelectedIndexChanged(object sender, EventArgs e)
        
{
            
this.cboDatabase.Items.Clear();
            
this.cboDatabase.Enabled = false;
            
this.groupBox1.Enabled = false;
            
this.lstObjects.Items.Clear();
        }


        
private void Form1_Load(object sender, EventArgs e)
        
{
            
try
            
{
                
//get all available SQL Servers                
                sqlServers = sqlApp.ListAvailableSQLServers();
                
for(int i=0;i<sqlServers.Count;i++)
                
{
                    
object srv = sqlServers.Item( i + 1);
                    
if(srv != null)
                    
{
                        
this.cboServers.Items.Add(srv);                        
                    }

                }

                
if(this.cboServers.Items.Count > 0)
                    
this.cboServers.SelectedIndex = 0;
                
else
                    
this.cboServers.Text = "<No available SQL Servers>";

            }

            
catch(Exception err)
            
{
                MessageBox.Show(err.Message,
"Error");
            }

        }


        
//Get Tables
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        
{
            
try
            
{
                
this.Cursor = Cursors.WaitCursor;
                
this.lstObjects.Items.Clear();
                
this.cboDatabase.Items.Clear();                
            
                SQLServer srv 
= new SQLServerClass();                
                srv.Connect(
this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);

                
foreach(Database db in srv.Databases)
                
{
                    
if(db.Name!=null)
                        
this.cboDatabase.Items.Add(db.Name);
                }

                
this.cboDatabase.Sorted = true;
                
if(this.cboDatabase.Items.Count >0)
                
{
                    
this.cboDatabase.SelectedIndex = 0;
                    
this.cboDatabase.Enabled = true;
                    
this.groupBox1.Enabled = true;
                }

                
else
                
{
                    
this.groupBox1.Enabled = false;
                    
this.cboDatabase.Enabled = false;
                    
this.cboDatabase.Text = "<No databases found>";
                }

                
this.Cursor = Cursors.Default;
            }

            
catch(Exception err)
            
{
                
this.Cursor = Cursors.Default;
                MessageBox.Show(err.Message,
"Error");
            }

        }

        

        
//Get stored procedures
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        
{
            
this.Cursor = Cursors.WaitCursor;
            SQLServer srv 
= new SQLServerClass();                
            srv.Connect(
this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
            
for(int i=0;i<srv.Databases.Count;i++)
            
{
                
if(srv.Databases.Item(i+1,"dbo").Name == this.cboDatabase.SelectedItem.ToString())
                
{
                    _Database db
= srv.Databases.Item(i+1,"dbo");
                    
this.lstObjects.Items.Clear();
                    
for(int j=0;j<db.Tables.Count;j++)
                    
{
                        
this.lstObjects.Items.Add(db.Tables.Item(j+1,"dbo").Name);
                    }

                    
this.Cursor = Cursors.Default;
                    
return;
                }

            }

            
this.Cursor = Cursors.Default;
        }

        
        
// Get Views
        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        
{
            
this.Cursor = Cursors.WaitCursor;
            SQLServer srv 
= new SQLServerClass();                
            srv.Connect(
this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
            
for(int i=0;i<srv.Databases.Count;i++)
            
{
                
if(srv.Databases.Item(i+1,"dbo").Name == this.cboDatabase.SelectedItem.ToString())
                
{
                    _Database db
= srv.Databases.Item(i+1,"dbo");
                    
this.lstObjects.Items.Clear();
                    
                    
for(int j=0;j<db.StoredProcedures.Count;j++)
                    
{
                        
this.lstObjects.Items.Add(db.StoredProcedures.Item(j+1,"dbo").Name);
                    }

                    
this.Cursor = Cursors.Default;
                    
return;
                }

            }

            
this.Cursor = Cursors.Default;
        }


        
private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        
{
            
this.Cursor = Cursors.WaitCursor;
            SQLServer srv 
= new SQLServerClass();                
            srv.Connect(
this.cboServers.SelectedText,this.txtUser.Text,this.txtPassword.Text);
            
for(int i=0;i<srv.Databases.Count;i++)
            
{
                
if(srv.Databases.Item(i+1,"dbo").Name == this.cboDatabase.SelectedItem.ToString())
                
{
                    _Database db
= srv.Databases.Item(i+1,"dbo");
                    
this.lstObjects.Items.Clear();
                    
for(int j=0;j<db.Views.Count;j++)
                    
{
                        
this.lstObjects.Items.Add(db.Views.Item(j+1,"dbo").Name);
                    }

                    
this.Cursor = Cursors.Default;
                    
return;
                }

            }

            
this.Cursor = Cursors.Default;
        }


        
private void cboDatabase_SelectedIndexChanged(object sender, EventArgs e)
        
{
            
this.lstObjects.Items.Clear();
        }


        
    }

}

posted on 2008-07-18 18:22  那片云  阅读(210)  评论(0编辑  收藏  举报

导航