在.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
新聞熱點
疑難解答