您的位置:首页 > 数据库

怎样获得Sqlserver 2000得实例列表和运行在一个实例上得数据库列表(转)

2006-05-13 09:33 489 查看
在.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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐