国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

怎樣獲得Sqlserver 2000得實例列表和運行在一個實例上得數據庫列表

2024-07-21 02:31:32
字體:
來源:轉載
供稿:網友

在.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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌拉特后旗| 陆川县| 马尔康县| 灌阳县| 林西县| 宁强县| 桃园县| 上犹县| 喀什市| 高唐县| 马边| 镇原县| 福州市| 河间市| 漳浦县| 万州区| 荆州市| 平山县| 密山市| 西和县| 荆州市| 绥中县| 镇坪县| 如皋市| 泸水县| 会昌县| 太仓市| 北海市| 仪陇县| 万荣县| 敦化市| 兴仁县| 平山县| 府谷县| 靖远县| 陕西省| 高雄市| 萨迦县| 沂南县| 富民县| 新巴尔虎左旗|