设为首页
加入收藏
帮助中心
首页 | 红盾通告 | 信息中心 | ASP技术 | 数据库 | 网页设计 | 网管专栏 | OICQ攻略 | 墨客频道 | 网站运营 |
当前位置:首页 >> 数据库 >> SQL Server >> 正文
最新信息
·SQL2000做附加数据库时报:…
·数据库sql被挂马终极批量删…
·动网论坛全文索引建立帮助
·论坛不支持文章内容搜索的…
·打造抵御SQL注入攻击的MSS…
·10个保护SQL Server 2000安…
·sql server安全设置攻略
·SQL Server安全设置清单列…
·使用 SQL Server 时需要经…
·SQL中触发器实例讲解
资料搜索
热点信息
·如何压缩SQL Server 2000数…
·SQL中触发器实例讲解
·日志文件满而造成SQL数据库…
·10个保护SQL Server 2000安…
·使用 SQL Server 时需要经…
·SQL2000做附加数据库时报:…
·SQL SERVER的安全隐患:触…
·asp如何连接sql server并实…
·SQL Server不能启动的常见…
·一种简单的表中删除重复行…
推荐信息
·打造抵御SQL注入攻击的MSS…
·SQL Server数据库开发的二…
·Sqlserver 优化的方法
·SQLServer吞噬的内存解决办…
·SQL SERVER日志清除的两种…
·如何压缩SQL Server 2000数…
·SQL Server 2000 SP4发布
·动网论坛全文索引建立帮助
·10个保护SQL Server 2000安…
·SQL Server安全设置清单列…


Google
 
怎样获得Sqlserver 2000得实例列表和运行在一个实例上得数据库列表
〖编辑:Cloudy | 浏览:人次〗

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


录入时间:2007-03-30 11:08:17 [打印本页] [关闭窗口] [返回顶部]
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。

Copyright © 2006-2014 0733168.Com Inc All Rights Reserved
关于我们 | 广告合作 | 联系我们 | 法律声明 | 友情链接 | 意见反馈
本站所收录信息、社区话题、及本站所做之广告均属其个人行为,与本站立场无关
湘ICP备06008436号