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

在.Net FrameWork中,能够很方便调用COM组件,有些时候我们需要获得运行在某个Sql Server上得服务实例列表和在一个实例上得数据库列表,通过Microsoft.SQLDMO.Object组件就可以轻松完成此项工作:
首先如何找到Microsoft.SQLDMO.Object
1.如何在您得项目中能够使用SQLDMO组件?
菜单-项目-添加引用-COM-Microsoft.SQLDMO.Object

2.将该功能写成一个类:

  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.Collections.Specialized;
  4 None.gif
  5 None.gif namespace  JillZhang
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// Summary description for SqlInfo.
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public class SqlInfo
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12ContractedSubBlock.gifExpandedSubBlockStart.gif        成员变量#region 成员变量
 13InBlock.gif        private string _uid="";
 14InBlock.gif        private string _pwd="";
 15InBlock.gif        private StringCollection _serverList=new StringCollection();
 16InBlock.gif        private Hashtable _databaseList=new Hashtable();
 17ExpandedSubBlockEnd.gif        #endregion

 18InBlock.gif
 19ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数#region 构造函数
 20InBlock.gif        public SqlInfo()
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            this._serverList=GetSqlInstances();    
 23InBlock.gif            if(this._serverList.Count>0)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 25InBlock.gif                foreach(string item in this._serverList)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 27InBlock.gif                    this._databaseList.Add(item,this.GetAllDatabases(item));
 28ExpandedSubBlockEnd.gif                }

 29ExpandedSubBlockEnd.gif            }

 30ExpandedSubBlockEnd.gif        }

 31InBlock.gif        public SqlInfo(string uid,string pwd)
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif            this._uid=uid;
 34InBlock.gif            this._pwd=pwd;
 35InBlock.gif            this._serverList=GetSqlInstances();    
 36InBlock.gif            if(this._serverList.Count>0)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 38InBlock.gif                foreach(string item in this._serverList)
 39ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 40InBlock.gif                    this._databaseList.Add(item,this.GetAllDatabases(item));
 41ExpandedSubBlockEnd.gif                }

 42ExpandedSubBlockEnd.gif            }

 43ExpandedSubBlockEnd.gif        }

 44ExpandedSubBlockEnd.gif        #endregion

 45InBlock.gif
 46ContractedSubBlock.gifExpandedSubBlockStart.gif        公共属性#region 公共属性
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 48InBlock.gif        /// 服务列表
 49ExpandedSubBlockEnd.gif        /// </summary>

 50InBlock.gif        public StringCollection ServerList
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52InBlock.gif            get
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 54InBlock.gif                return _serverList;
 55ExpandedSubBlockEnd.gif            }

 56ExpandedSubBlockEnd.gif        }

 57ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 58InBlock.gif        /// 用于登录Sql server得用户名
 59ExpandedSubBlockEnd.gif        /// </summary>

 60InBlock.gif        public string Uid
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            get
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 64InBlock.gif                return _uid;
 65ExpandedSubBlockEnd.gif            }

 66InBlock.gif            set
 67ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 68InBlock.gif                _uid=value;
 69ExpandedSubBlockEnd.gif            }

 70ExpandedSubBlockEnd.gif        }

 71ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 72InBlock.gif        /// 用于登录得密码
 73ExpandedSubBlockEnd.gif        /// </summary>

 74InBlock.gif        public string Pwd
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            get
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                return _pwd;
 79ExpandedSubBlockEnd.gif            }

 80InBlock.gif            set
 81ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 82InBlock.gif                _pwd=value;
 83ExpandedSubBlockEnd.gif            }

 84ExpandedSubBlockEnd.gif        }

 85ExpandedSubBlockEnd.gif        #endregion

 86InBlock.gif
 87ContractedSubBlock.gifExpandedSubBlockStart.gif        事件方法#region 事件方法
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 89InBlock.gif        /// 获得服务列表
 90InBlock.gif        /// </summary>
 91ExpandedSubBlockEnd.gif        /// <returns></returns>

 92InBlock.gif        public static System.Collections.Specialized.StringCollection GetSqlInstances()
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 94InBlock.gif            //声明一个字符串链表,用于存放列表信息
 95InBlock.gif            System.Collections.Specialized.StringCollection instaces= new System.Collections.Specialized.StringCollection();
 96InBlock.gif            try
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 98InBlock.gif                //以下代码是通过调用SQLDMO组件来实现获得服务列表
 99InBlock.gif                SQLDMO.Application sqlApplication= new SQLDMO.ApplicationClass();
100InBlock.gif                SQLDMO.NameList sqlServerIntances=sqlApplication.ListAvailableSQLServers();
101InBlock.gif                for(int i=0;i<sqlServerIntances.Count;i++)
102ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
103InBlock.gif                    object svr=sqlServerIntances.Item(i+1);//索引从1开始
104InBlock.gif                    if(svr!=null)
105ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
106InBlock.gif                        instaces.Add(svr.ToString());
107ExpandedSubBlockEnd.gif                    }

108ExpandedSubBlockEnd.gif                }

109InBlock.gif                return instaces;
110ExpandedSubBlockEnd.gif            }

111InBlock.gif            catch
112ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
113InBlock.gif                throw new Exception("未能获得Server得列表信息,请查看您得Sql server是否正在运行!");
114ExpandedSubBlockEnd.gif            }

115ExpandedSubBlockEnd.gif        }

116InBlock.gif
117ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
118InBlock.gif        /// 获得Sqlserver 2000一个Server Instance上得数据库列表
119InBlock.gif        /// </summary>
120InBlock.gif        /// <param name="server">服务名称</param>
121InBlock.gif        /// <param name="uid">登录用户</param>
122InBlock.gif        /// <param name="pwd">登录密码</param>
123ExpandedSubBlockEnd.gif        /// <returns>数据库列表</returns>

124InBlock.gif        public static System.Collections.Specialized.StringCollection GetAllDatabases(string server,string uid,string pwd)
125ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
126InBlock.gif            System.Collections.Specialized.StringCollection databases= new System.Collections.Specialized.StringCollection();
127InBlock.gif            try
128ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
129InBlock.gif                SQLDMO.SQLServer sqlServer =new  SQLDMO.SQLServerClass();
130InBlock.gif                sqlServer.Connect(server,uid,pwd);
131InBlock.gif                foreach(SQLDMO.Database db in sqlServer.Databases)
132ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
133InBlock.gif                    if(db.Name!=null)
134ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
135InBlock.gif                        databases.Add(db.Name);
136ExpandedSubBlockEnd.gif                    }

137ExpandedSubBlockEnd.gif                }

138InBlock.gif                return databases;
139ExpandedSubBlockEnd.gif            }

140InBlock.gif            catch
141ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
142InBlock.gif                throw new Exception("未能获得服务"+server+"上得数据库列表,可能您得服务器没有启动或者您得用户名或密码错误");
143ExpandedSubBlockEnd.gif            }

144ExpandedSubBlockEnd.gif        }

145InBlock.gif        public System.Collections.Specialized.StringCollection GetAllDatabases(string server)
146ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
147InBlock.gif            return GetAllDatabases(server,this._uid,this._pwd);
148ExpandedSubBlockEnd.gif        }

149ExpandedSubBlockEnd.gif        #endregion

150InBlock.gif        
151ExpandedSubBlockEnd.gif    }

152ExpandedBlockEnd.gif}

153 None.gif

 
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值