Hướng dẫn sử dụng SQLDMO duyệt sqlserver trên máy

Hôm nay tìm hiểu trên mạng cách duyệt tìm các cơ sở dữ liệu có trong máy.

Mình đã làm được và viết bài hướng dẫn cho các bạn cùng tham khảo.
Xem hình dưới :



Đầu tiên các bạn cần khai báo SQLDMO :

Add Preferences -> Com -> Microsoft SQLDMO object Library
using SQLDMO;
//Load các sql avaible trên máy
  private void Form1_Load(object sender, EventArgs e)
        {
            SQLDMO.Application sqlA = new SQLDMO.Application();
            SQLDMO.NameList sqlNameList = sqlA.ListAvailableSQLServers();
            for (int i = 0; i < sqlNameList.Count; i++)
            {
                object srv = sqlNameList.Item(i + 1);
                if (srv != null)
                {
                    this.comboBox1.Items.Add(srv);
                }
            }           
            if (this.comboBox1.Items.Count > 0)
                this.comboBox1.SelectedIndex = 0;
            else
                this.comboBox1.Text = "<No available SQL Servers>";           
        }  
// btt Duyet tim
// Duyệt tìm các database trong SQL kết nối ở trên
private void button1_Click(object sender, EventArgs e)
        {
            
            try
            {
                comboBox2.Items.Clear();
                SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
                SQLDMO.SQLServer srv1 = new SQLDMO.SQLServerClass();       
                if (radioButton1.Checked == true)
                {
                    srv1.Name = comboBox1.Text;
                    srv1.LoginSecure = true;
                    srv1.Connect(null, null, null);
                    //srv1.Connect(this.comboBox1.SelectedItem.ToString(), "sa", "123456");
                    foreach (SQLDMO.Database db in srv1.Databases)
                    {
                        if (db.Name != null)
                            this.comboBox2.Items.Add(db.Name);
                    }
                }
                else
                {
                    
                    srv1.Connect(this.comboBox1.Text.ToString(),textBox2.Text, textBox3.Text);
                    foreach (SQLDMO.Database db in srv1.Databases)
                    {
                        if (db.Name != null)
                            this.comboBox2.Items.Add(db.Name);
                    }
                }
            }
            catch
            { }
        }
Demo :
http://www.mediafire.com/?dgbvlt1w9raa8

Bạn nào có thắc mắc hay muốn hỏi.Vui lòng để lại comment trong bài viết.



SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment