日期:2013-04-27  浏览次数:20633 次

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