怎樣獲得Sqlserver 2000得執行個體列表和運行在一個執行個體上得資料庫列表

來源:互聯網
上載者:User
server|sqlserver|資料|資料庫

在.Net FrameWork中,能夠很方便調用COM組件,有些時候我們需要獲得運行在某個Sql Server上得服務執行個體列表和在一個執行個體上得資料庫列表,通過Microsoft.SQLDMO.Object組件就可以輕鬆完成此項工作:
首先如何找到Microsoft.SQLDMO.Object
1.如何在您得項目中能夠使用SQLDMO組件?
菜單-項目-添加引用-COM-Microsoft.SQLDMO.Object

2.將該功能寫成一個類:


  1using System;
  2using System.Collections;
  3using System.Collections.Specialized;
  4
  5namespace JillZhang
  6{
  7    /**//// <summary>
  8    /// Summary description for SqlInfo.
  9    /// </summary>
 10    public class SqlInfo
 11    {
 12        成員變數#region 成員變數
 13        private string _uid="";
 14        private string _pwd="";
 15        private StringCollection _serverList=new StringCollection();
 16        private Hashtable _databaseList=new Hashtable();
 17        #endregion
 18
 19        建構函式#region 建構函式
 20        public SqlInfo()
 21        {
 22            this._serverList=GetSqlInstances();   
 23            if(this._serverList.Count>0)
 24            {
 25                foreach(string item in this._serverList)
 26                {
 27                    this._databaseList.Add(item,this.GetAllDatabases(item));
 28                }
 29            }
 30        }
 31        public SqlInfo(string uid,string pwd)
 32        {
 33            this._uid=uid;
 34            this._pwd=pwd;
 35            this._serverList=GetSqlInstances();   
 36            if(this._serverList.Count>0)
 37            {
 38                foreach(string item in this._serverList)
 39                {
 40                    this._databaseList.Add(item,this.GetAllDatabases(item));
 41                }
 42            }
 43        }
 44        #endregion
 45
 46        公用屬性#region 公用屬性
 47        /**//// <summary>
 48        /// 服務列表
 49        /// </summary>
 50        public StringCollection ServerList
 51        {
 52            get
 53            {
 54                return _serverList;
 55            }
 56        }
 57        /**//// <summary>
 58        /// 用於登入Sql server得使用者名稱
 59        /// </summary>
 60        public string Uid
 61        {
 62            get
 63            {
 64                return _uid;
 65            }
 66            set
 67            {
 68                _uid=value;
 69            }
 70        }
 71        /**//// <summary>
 72        /// 用於登入得密碼
 73        /// </summary>
 74        public string Pwd
 75        {
 76            get
 77            {
 78                return _pwd;
 79            }
 80            set
 81            {
 82                _pwd=value;
 83            }
 84        }
 85        #endregion
 86
 87        事件方法#region 事件方法
 88        /**//// <summary>
 89        /// 獲得服務列表
 90        /// </summary>
 91        /// <returns></returns>
 92        public static System.Collections.Specialized.StringCollection GetSqlInstances()
 93        {
 94            //聲明一個字串鏈表,用於存放列表資訊
 95            System.Collections.Specialized.StringCollection instaces= new System.Collections.Specialized.StringCollection();
 96            try
 97            {
 98                //以下代碼是通過調用SQLDMO組件來實現獲得服務列表
 99                SQLDMO.Application sqlApplication= new SQLDMO.ApplicationClass();
100                SQLDMO.NameList sqlServerIntances=sqlApplication.ListAvailableSQLServers();
101                for(int i=0;i<sqlServerIntances.Count;i++)
102                {
103                    object svr=sqlServerIntances.Item(i+1);//索引從1開始
104                    if(svr!=null)
105                    {
106                        instaces.Add(svr.ToString());
107                    }
108                }
109                return instaces;
110            }
111            catch
112            {
113                throw new Exception("未能獲得Server得列表資訊,請查看您得Sql server是否正在運行!");
114            }
115        }
116
117        /**//// <summary>
118        /// 獲得Sqlserver 2000一個Server Instance上得資料庫列表
119        /// </summary>
120        /// <param name="server">服務名稱</param>
121        /// <param name="uid">登入使用者</param>
122        /// <param name="pwd">登入密碼</param>
123        /// <returns>資料庫列表</returns>
124        public static System.Collections.Specialized.StringCollection GetAllDatabases(string server,string uid,string pwd)
125        {
126            System.Collections.Specialized.StringCollection databases= new System.Collections.Specialized.StringCollection();
127            try
128            {
129                SQLDMO.SQLServer sqlServer =new  SQLDMO.SQLServerClass();
130                sqlServer.Connect(server,uid,pwd);
131                foreach(SQLDMO.Database db in sqlServer.Databases)
132                {
133                    if(db.Name!=null)
134                    {
135                        databases.Add(db.Name);
136                    }
137                }
138                return databases;
139            }
140            catch
141            {
142                throw new Exception("未能獲得服務"+server+"上得資料庫列表,可能您得伺服器沒有啟動或者您得使用者名稱或密碼錯誤");
143            }
144        }
145        public System.Collections.Specialized.StringCollection GetAllDatabases(string server)
146        {
147            return GetAllDatabases(server,this._uid,this._pwd);
148        }
149        #endregion
150       
151    }
152}
153



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.